1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input id="box" type="text">
<button id="addition">+</button>
<button id="add">=</button>
<button id="reset">reset</button>
<p id="result"></p>
<script>
var inputText = window.document.querySelector("#box");
var buttonAddition = window.document.querySelector("#addition");
var buttonAdd = window.document.querySelector("#add");
var result = window.document.querySelector("#result");
var reset = window.document.querySelector("#reset");
var number1, number2, sum;
buttonAddition.addEventListener("click", function()
{
number1 = inputText.value;
inputText.value = "";
});
buttonAdd.addEventListener("click", function()
{
number2 = inputText.value;
sum = Number(number1) + Number(number2);
result.textContent = sum;
});
reset.addEventListener("click", function()
{
});
</script>
</body>
</html>
Good in the above example has a very simple calculator. But a doubt I have and is not today would be like deleting character by character within the input
, for example in this calculator the user has entered the number 1500
, by pressing the button once reset
it will erase the last character, if I press twice it erases the last 2 and so it will always remove the last character from the value of the input
when the button is pressed reset
. How would I do that?.
You’re the man! , perfect was just what I wanted, thank you!
– Leandro Nascimento