Delete last entered digit

-2

I'm creating a calculator, using HTML and JavaScript. My goal was that it had a button that allows the user to delete the last value entered (for example, the user types the number 9 and then 8 and form 98 , but he wants to delete the%

I would like a JavaScript code to solve this problem.

    
asked by anonymous 21.08.2018 / 16:13

2 answers

1

Use method substring , input.value = inputText.substring(0,inputText.length-1);

<input id="input" type="text"/>
<button onclick="apagar()">Apagar</button>

<script>
function apagar() {
  let input = document.getElementById('input');
  let inputText = input.value;
  input.value = inputText.substring(0,inputText.length-1);
  console.log(input.value);
}</script>
    
21.08.2018 / 16:21
1

You can use the slice function:

var str=document.getElementById("campo-com-o-valor").value;
str = str.slice(0, -1);
    
21.08.2018 / 16:24