Jump to content

Calculating my check box variables to Sum a total??


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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.