I am working on a checking form assignment. So, i have 3 products with price and the type is checkbox. The total field will display the total cost from only the checked products. However, when i click check the box the total field display nothing. Am I missing something? Any idea? Thank you! My HTML and JS:
<form action="" id="theForm">
<fieldset>
<legend>
Products
</legend>
<label>
<input name="product" value="12.95" type="checkbox" id="p1" onclick="total()"/>
Candy $12.95
</label>
<label>
<input name="product" value="5.99" type="checkbox" id="p2" onclick="total()"/>
Burger $5.99
</label>
<label>
<input name="product" value="1.99" type="checkbox" id="p3" onclick="total()"/>
Coke $1.99
</label>
<label>
Total
<input value="$0.00" readonly="readonly" type="text" id="total"/>
</label>
</fieldset>
<input value="Submit" type="submit"/>
<input value="Reset" type="reset"/>
</form>
JS
function total() {
var input = document.getElementById("form");
var total = 0;
for (var i = 0; i < input.length; i++) {
if (input[i].checked) {
total += parseFloat(input[i].value);
}
}
document.getElementById("total").value = "$" + total;
}
var input = document.getElementsByName("product");should work better - you have nothing with ID="form" and if you loop over theForm, you will get the total and buttons in the loop too