I have a html table of 3 columns, the first 2 are <input type="number">
and the third is text.
I want to play in the third the sum of the values of inputs
using onchange
, but what I am facing is that if I put "2" in each input
, the sum in the third column is 6, since the previous value of input
influences final result.
How do I solve this problem ??
Below my code.
<table>
<tr>
<td>valor 1</td>
<td>valor 2</td>
<td>total</td>
</tr>
<tr>
<td><input type="number" min="0" onchange="calcula(this.value)"></td>
<td><input type="number" min="0" onchange="calcula(this.value)"></td>
<td><input type="text" readonly value="0" id="teste"></td>
</tr>
</table>
<script type="text/javascript">
function calcula(valor){
var soma = parseInt(document.getElementById("teste").value);
soma += parseInt(valor);
document.getElementById("teste").value = soma;
}
</script>