0
I would like to add the values informed in two fields/inputs click on the button and the answer appears in another field/input. I can’t figure out what’s missing in the code below.
<!DOCTYPE HTML>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>JS Examples</title>
</head>
<body>
<input type="text" id="soma1" maxlength="5"><br><br>
<input type="text" id="soma2" maxlength="5"><br><br>
<button id="btn">Resultado</button><br><br>
<input type="text" id="resultado" maxlength="5">
</body>
</html>
<script type="text/javascript">
var soma1 = document.getElementById('soma1');
var soma2 = document.getElementById('soma2');
var btn = document.getElementById('btn');
var result = document.getElementById('resultado');
soma1 = parseInt(soma1.value);
soma2 = parseInt(soma2.value);
btn.addEventListener('click', function(){
somatotal();
});
function somatotal(){
var soma = soma1 + soma2;
document.getElementById('resultado').value = soma;
}
</script>
See help: https://answall.com/questions/173458/substr-ancora-por-bot%C3%A3o-em-formul%C3%A1rio-com-c%C3%A1lculo-pelo-javascript
– Laércio Lopes
In the answer of this question I passed you have an important information on conversion of value reported in imput.
– Laércio Lopes
Thanks for the tip Laércio!
– Thiago Pernomian