944,134 Members | Top Members by Rank

Ad:
May 7th, 2007
0

An array of arrays of of radio buttons

Expand Post »
OK, I know how to address all of the radio buttons on a page as an array, and address all of the radio buttons in a single group as an array. And I know how to create multidimensional objects in JavaScript.

But how can I create a two-dimensional array of radio buttons and address it in JavaScript?

Let's do a simple set of four groups of radio buttons, each with three selections (the actual set I want to use is much larger, but I can scale up the solution). Each group can have only one selection.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. Group 1: O A O B O C
  2. Group 2: O A O B O C
  3. Group 3: O A O B O C
  4. Group 4: O A O B O C
I want to use for loops to count the number of A selections, the number of B selections, and the number of C selections. I also want to look for diagonal lines of three checked buttons.

What I can't figure out is how to name the button groups in the HTML code so I can address all of them in the loops, yet keep them separate for the purpose of having one radio button selection in each group.

I don't want to use the global array of radio buttons on the page, because the number of buttons above this structure in the webpage will vary. I also want to address them in x-y notation.
Last edited by MidiMagic; May 7th, 2007 at 3:42 am. Reason: I can't edit the title????
Similar Threads
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
May 8th, 2007
0

Re: An array of arrays of of radio buttons

Nobody knows this?

Also tell me if you don't see any way to do it.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
May 8th, 2007
0

Re: An array of arrays of of radio buttons

I solved it myself.

You build an array of objects, then index that.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Try out the radio</title>
  4.  
  5. <style type="text/css">
  6. <!--
  7.  
  8. .cenx {text-align: center;}
  9. .wfd {width: 100%}
  10.  
  11. img {padding: 24pt;}
  12. body {background-color: #cccccc; padding: 2%; font-family: Arial, sans-serif;}
  13.  
  14. -->
  15. </style>
  16.  
  17. <script type="text/javascript">
  18.  
  19. function sumitup(){
  20. var xax = [document.forms.vote.appr1c, document.forms.vote.appr2c, document.forms.vote.appr3c, document.forms.vote.appr4c];
  21. var sum, grp, x, y;
  22. var acc = Array(3);
  23.  
  24. sum = 0;
  25. for(x = 0; x < 3; x++){
  26. acc[x] = 0;
  27. for(y = 0; y < xax.length; y++){
  28. grp = xax[y];
  29. if(grp[x].checked){
  30. sum = sum + 1;
  31. acc[x] = acc[x] + 1;
  32. };
  33. };
  34. };
  35. alert('A: ' + acc[0] + ', B: ' + acc[1] + ', C: ' + acc[2] + ', TOTAL: ' + sum);
  36. };
  37.  
  38. </script>
  39.  
  40. </head>
  41. <body>
  42.  
  43. <form name="vote" id="vote" action="none">
  44. <p><fieldset>
  45. <legend>Block 1</legend>
  46. <input type="radio" name="appr1c" value="yes" />A
  47. <input type="radio" name="appr1c" value="yes" />B
  48. <input type="radio" name="appr1c" value="yes" />C
  49. </fieldset></p>
  50.  
  51. <p><fieldset>
  52. <legend>Block 2</legend>
  53. <input type="radio" name="appr2c" value="yes" />A
  54. <input type="radio" name="appr2c" value="yes" />B
  55. <input type="radio" name="appr2c" value="yes" />C
  56. </fieldset></p>
  57.  
  58. <p><fieldset>
  59. <legend>Block 2</legend>
  60. <input type="radio" name="appr3c" value="yes" />A
  61. <input type="radio" name="appr3c" value="yes" />B
  62. <input type="radio" name="appr3c" value="yes" />C
  63. </fieldset></p>
  64.  
  65. <p><fieldset>
  66. <legend>Block 2</legend>
  67. <input type="radio" name="appr4c" value="yes" />A
  68. <input type="radio" name="appr4c" value="yes" />B
  69. <input type="radio" name="appr4c" value="yes" />C
  70. </fieldset></p>
  71.  
  72. <input type="button" id="startx" name="startx" value="GO" onclick="javascript:sumitup()" />
  73. </form>
  74. </body>
  75. </html>
Last edited by MidiMagic; May 8th, 2007 at 3:00 pm.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Frames
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: JS generated page 'save as' issues





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC