Calculating Totals
HTML
<input type="text" id="item_subtotal[1]" value=""><br>
<input type="text" id="item_subtotal[2]" value=""><br>
<input type="text" id="item_subtotal[3]" value=""><br>
<input type="text" id="item_subtotal[4]" value=""><br>
<input type="text" id="item_subtotal[5]" value=""><br>
Total: <span id="order_total">?</span>
<button onClick="calculateTotal(5)">Update</button>
JavaScript
function calculateTotal(numitems) {
var totalAmt = 0;
for (var cnt = 1; cnt <= numitems; cnt++) {
var subtotal = document.getElementById('item_subtotal[' + cnt + ']');
if (subtotal.value === null || subtotal.value === '') {
continue;
}
totalAmt += (subtotal.value * 1);
}
document.getElementById('order_total').innerHTML = totalAmt;
}