An array of arrays of of radio buttons

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

An array of arrays of of radio buttons

 
0
  #1
May 7th, 2007
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????
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: An array of arrays of of radio buttons

 
0
  #2
May 8th, 2007
Nobody knows this?

Also tell me if you don't see any way to do it.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: An array of arrays of of radio buttons

 
0
  #3
May 8th, 2007
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.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC