Jump to content

Calculating my check box variables to Sum a total??


suess0r

Recommended Posts

Hi there,

I have a site with my advertisement packages listed on them, with checkboxes as values (found at: http://www.clb411.com/adv.php) When they click on one of the packages I would like it if the subtotal text field will be filled with the sum of the packages onClick of the checkboxes. I would assume I would fill the $textamount with an array that updates onclick somehow but i'm not sure exactly the best way to go about this... can anyone point me in a better direction?
nevermind i solved it myself... here's the code if anyone was interested

[quote]<script type="text/javascript">
function checkTotal() {
document.listForm.total.value = '';
var sum = 0;
for (i=0;i<document.listForm.choice.length;i++) {
  if (document.listForm.choice[i].checked) {
  sum = sum + parseInt(document.listForm.choice[i].value);
  }
}
document.listForm.total.value = sum;
}
</script>

<form name="listForm">
<input type="checkbox" name="choice" value="2" onchange="checkTotal()"/>2<br/>
<input type="checkbox" name="choice" value="5" onchange="checkTotal()"/>5<br/>
<input type="checkbox" name="choice" value="10" onchange="checkTotal()"/>10<br/>
<input type="checkbox" name="choice" value="20" onchange="checkTotal()"/>20<br/>
Total: <input type="text" size="2" name="total" value="0"/>
</form>[/quote]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.