Submit a sum online

Asked

Viewed 58 times

-1

Good afternoon! I have a form and a routine in JAVASCRIPT. My wish is that, from a typing, automatically appear the sum value. An example I’m looking for is the YOUSE, where by changing the value of the insurance covers, automatically the cost is displayed (without "TAB", "BUTTONS").

In my example, when you change, only with the TAB in N2 that updates. Another point, which despite being "pre-filled", is not available for example the sum of N1 and N2 (in case - 50).

FORM:

<input type="number" id="n1" value="20">
<input type="number" id="n2" value="30" onblur="calcularonline()"> 
<input type="text" id="n3" value="100">
<input name="idade" type="number" placeholder=0 id="id_idade">
<div id="resultado"</div>
<div id="resultado2"</div>

JAVASCRIPT

function calcularonline() {
  var n1 = parseInt(document.getElementById('n1').value, 10);
  var n2 = parseInt(document.getElementById('n2').value, 10);
  document.getElementById('resultado').innerHTML = n1 + n2;

  var n3 = parseInt(document.getElementById('n3').value, 10);
  var n4 = (parseInt(document.getElementById('id_idade').value, 10))*2;
  var n5 = 0;

  if(n4>=10){
      n5 = 10000;
  }
  document.getElementById('resultado2').innerHTML = n3 + n4 + n5;
}

1 answer

0

Hello, see if that’s what you need:

function Somar() {
    var num1 = document.getElementById("num1").value;
    var num2 = document.getElementById("num2").value;

    if (num1 == '') {
        num1 = 0;
    }

    if (num2 == '') {
        num2 = 0;
    }

    var resultado = parseInt(num1) + parseInt(num2);    
    document.getElementById("resultado").innerHTML = resultado;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <title>Exemplo</title>
    <script src='script.js'></script>
</head>
<body>
    <p>Escreva algum número para ativar a função.</p>
    <input type="text" id="num1" oninput="Somar()">
    <input type="text" id="num2" oninput="Somar()">
    <p id="resultado"></p>    
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.