troubleshooting Question

Add / Subtract checkboxes in an array. Javascript

Avatar of ussher
ussherFlag for Japan asked on
JavaScript
3 Comments1 Solution498 ViewsLast Modified:
hi,

i want to add / subtract the checkboxes of a table so the amount is totaled at the bottom of the page.

I also need to be able to send the checked boxes as an array so the name of the input has to be something like  'choice[]'

i have found a scritp here that almost works:
http://www.madirish.net/?article=7

<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>

but this script does not allow me to change the name from choice to choice[ ].

if the name of the input can become choice[] and the totals still calculate then i have what  i need.

thanks

ussher
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros