1
I have 2 input fields and need to add the value of their contents when typing, but I noticed that when I type the first value in the first input appears NaN
only when I type something in the second input, it starts to add up, so if I have 5 inputs, I would have to follow in all to have a sum.
I want it to add up as I enter the values in the input, without needing to fill them all, if I fill in some already makes the sum,
My Code:
<html>
<head>
<title></title>
<script type="text/javascript">
function somar()
{
var campo_1 = document.getElementById("campo_1").value;
var campo_2 = document.getElementById("campo_2").value;
var campo_3 = document.getElementById("campo_3").value;
var campo_4 = document.getElementById("campo_4").value;
var campo_5 = document.getElementById("campo_5").value;
var somar = parseInt(campo_1) + parseInt(campo_2) + parseInt(campo_3) + parseInt(campo_4) + parseInt(campo_5);
console.log(somar);
}
</script>
<body>
<input id="campo_1" onkeydown="somar();" type="text"></input>
<input id="campo_2" onkeydown="somar();" type="text"></input>
<input id="campo_3" onkeydown="somar();" type="text"></input>
<input id="campo_4" onkeydown="somar();" type="text"></input>
<input id="campo_5" onkeydown="somar();" type="text"></input>
</body>
</html>
That’s what I’ve been looking for, thank you
– abduzeedo