By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,301 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

looping through radio buttons then adding the total

P: 4
Hi,

hope someone can help, I need to be able to keep a running total of radio buttons selected, as a user goes through a set of 16 questions, devided in to 4 catorgories, then on "submit" have the total number showen on screen, or on "submit" loop through the selected radio buttons and get the total.

What I have at the moment will loop through all the buttons and just add all the totals, how do I get this code to add together the 'checked' buttons only?

Thanks Alan :)

Expand|Select|Wrap|Line Numbers
  1. ..
  2.  
  3. <script type="text/javascript">
  4.  
  5. function showTotal(form, totalEl)
  6. {
  7. var el, els = form.elements;
  8. var sum = 0;
  9.  
  10. for (var i=0, num=els.length; i<num; ++i){
  11. el = els[i];
  12.  
  13. // need to only add up checked radio buttons?
  14. if ('radio' == el.type && /SumB/.test(el.name)){
  15. sum += +el.value;
  16. }
  17. form.elements[totalEl].value = sum;
  18. }
  19. }
  20. </script>
  21. </head>
  22.  
  23. <body>
  24.  
  25.  
  26. <form action=""><div>
  27. First set of questions:<br>
  28. Question group 1: <input type="radio" name="SumB_myquestion1" value="5" checked><br>
  29. Question group 1: <input type="radio" name="SumB_myquestion1" value="10"><br>
  30. Question group 1: <input type="radio" name="SumB_myquestion1" value="15"><br>
  31. Question group 1: <input type="radio" name="SumB_myquestion1" value="20"><br>
  32. <br>
  33. Second set of questions:<br>
  34. Question group 2: <input type="radio" name="SumB_myquestion2" value="5" checked><br>
  35. Question group 2: <input type="radio" name="SumB_myquestion2" value="10"><br>
  36. Question group 2: <input type="radio" name="SumB_myquestion2" value="15"><br>
  37. Question group 2: <input type="radio" name="SumB_myquestion2" value="20"><br>
  38. <br>
  39.  
  40. <input type="button" value="Show total" onclick="showTotal(this.form, 'formTotal');">&nbsp;
  41. <br>
  42. <br>
  43.  
  44. <input type="text" name="formTotal" readonly size="20"> is your score.<br>
  45. <br>
  46.  </div></form> 
  47. ..
Oct 5 '07 #1
Share this Question
Share on Google+
5 Replies


dmjpro
100+
P: 2,476
Hi,

hope someone can help, I need to be able to keep a running total of radio buttons selected, as a user goes through a set of 16 questions, devided in to 4 catorgories, then on "submit" have the total number showen on screen, or on "submit" loop through the selected radio buttons and get the total.

What I have at the moment will loop through all the buttons and just add all the totals, how do I get this code to add together the 'checked' buttons only?

Thanks Alan :)

Expand|Select|Wrap|Line Numbers
  1. ..
  2.  
  3. <script type="text/javascript">
  4.  
  5. function showTotal(form, totalEl)
  6. {
  7. var el, els = form.elements;
  8. var sum = 0;
  9.  
  10. for (var i=0, num=els.length; i<num; ++i){
  11. el = els[i];
  12.  
  13. // need to only add up checked radio buttons?
  14. if ('radio' == el.type && /SumB/.test(el.name)){
  15. sum += +el.value;
  16. }
  17. form.elements[totalEl].value = sum;
  18. }
  19. }
  20. </script>
  21. </head>
  22.  
  23. <body>
  24.  
  25.  
  26. <form action=""><div>
  27. First set of questions:<br>
  28. Question group 1: <input type="radio" name="SumB_myquestion1" value="5" checked><br>
  29. Question group 1: <input type="radio" name="SumB_myquestion1" value="10"><br>
  30. Question group 1: <input type="radio" name="SumB_myquestion1" value="15"><br>
  31. Question group 1: <input type="radio" name="SumB_myquestion1" value="20"><br>
  32. <br>
  33. Second set of questions:<br>
  34. Question group 2: <input type="radio" name="SumB_myquestion2" value="5" checked><br>
  35. Question group 2: <input type="radio" name="SumB_myquestion2" value="10"><br>
  36. Question group 2: <input type="radio" name="SumB_myquestion2" value="15"><br>
  37. Question group 2: <input type="radio" name="SumB_myquestion2" value="20"><br>
  38. <br>
  39.  
  40. <input type="button" value="Show total" onclick="showTotal(this.form, 'formTotal');">&nbsp;
  41. <br>
  42. <br>
  43.  
  44. <input type="text" name="formTotal" readonly size="20"> is your score.<br>
  45. <br>
  46.  </div></form> 
  47. ..
Check this line .....
Expand|Select|Wrap|Line Numbers
  1. sum = 0; //sum must be initialized as zero
  2. .
  3. .
  4. //sum += +el.value; .... what does it mean?
  5. //i think it will be .......
  6. sum += parseInt(el.value);
  7. //or
  8. sum += parseFloat(el.value); //if your fraction number system is there
  9.  
Debasis Jana
Oct 5 '07 #2

P: 4
Check this line .....
Expand|Select|Wrap|Line Numbers
  1. sum = 0; //sum must be initialized as zero
  2. .
  3. .
  4. //sum += +el.value; .... what does it mean?
  5. //i think it will be .......
  6. sum += parseInt(el.value);
  7. //or
  8. sum += parseFloat(el.value); //if your fraction number system is there
  9.  
Debasis Jana
Hi,

I have changed that as suggested but I still get the same answer '100' which is the total of all radio buttons added together, There may be a better way of doing this, and I am open to suggestions!!

Alan
Oct 5 '07 #3

gits
Expert Mod 5K+
P: 5,333
hi ...

you always add all values ... check for the checked property too:

Expand|Select|Wrap|Line Numbers
  1. if ('radio' == el.type && /SumB/.test(el.name) && el.checked) {
  2.     // your code
  3. }
using the + is equivalent to parseFloat or parseInt ... it forces a typecast, so your first posted code should work with that change. but using the correct parse-method simplifies reading :)

kind regards
Oct 5 '07 #4

P: 4
All working now, thanks for your help and support.

Alan
Oct 5 '07 #5

gits
Expert Mod 5K+
P: 5,333
hi ...

glad to hear you got it working :) ... post back to the forum anytime you have more questions ...

kind regards
Oct 5 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.