JSFiddle - React, Tailwind, and code Playground
by j08691
HTML
value1:
<input type="text" id="total_1">
<br>value2:
<input type="text" id="total_2">
<br>value3:
<input type="text" id="total_3">
<br>value4:
<input type="text" id="total_4">
<br>value5:
<input type="text" id="total_5">
<br>total:
<input type="text" id="totalresult">
<button type="button" onclick="getTotal(); return false;">Get total</button>
JavaScript
function getTotal() {
var sum = 0;
for (i = 1; i <= 5; i++) {
var total = document.getElementById('total_' + i).value;
if (!isNaN(parseFloat(total))) sum = parseFloat(total) + sum;
document.getElementById('totalresult').value = sum;
}
}