I am trying to get the total of the checkboxes selected when the user clicks the "calculate total" button. It isn't working though. What am I doing wrong?
<html>
<head>
<script type="text/javascript">
function calculate(f)
{
var nums = f.num;
var ntext = f.numtext;
var nitem = f.numitem;
var result = 0;
var items = '';
for(var i=0;i<nums.length;i++)
{
if(nums[i].checked)
{
result+=parseFloat(ntext[i].value);
items+=nitem[i].value+'\n';
}
}
f.answer.value=result;
}
if (nums[i].checked) {
window.alert("Your total is " + f.answer.value=result;);
}
</script>
</head>
<body>
<style type="text/css">
.box1 { float:left;
width:500px;
clear:left;
}
.box2 { float:left;
padding-left:50px;
}
</style>
<form name="myform">
Build your own family meal<br>
<div class="box1">
<input type="checkbox" name="num" onclick="calculate(this.form)">
<input type="hidden" name="numtext" value="24.00" onchange="calculate(this.form)">
<input type="hidden" name="numitem" value="pencil" onchange="calculate(this.form)">
<div class="box2">Chicken Enchiladas $24.00</div>
</div><br>
<div class="box1">
<input type="checkbox" name="num" onclick="calculate(this.form)">
<input type="hidden" name="numtext" value="15.00" onchange="calculate(this.form)">
<input type="hidden" name="numitem" value="pen" onchange="calculate(this.form)">
<div class="box2">Rice and Beans $15.00</div>
</div><br>
<div class="box1">
<input type="checkbox" name="num" onclick="calculate(this.form)">
<input type="hidden" name="numtext" value="6.00" onchange="calculate(this.form)">
<input type="hidden" name="numitem" value="paper" onchange="calculate(this.form)">
<div class="box2">Chips and Salsa $6.00</div>
</div><br>
<div class="box1">
<input type="submit" name="answer" value="Calculate Total" onclick="calculate(f)" />
</div>
</form>
</body>
</html>