| | |
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 |
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.
I want to use
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.
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)
Group 1: O A O B O C Group 2: O A O B O C Group 3: O A O B O C Group 4: O A O B O C
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
I solved it myself.
You build an array of objects, then index that.
You build an array of objects, then index that.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <title>Try out the radio</title> <style type="text/css"> <!-- .cenx {text-align: center;} .wfd {width: 100%} img {padding: 24pt;} body {background-color: #cccccc; padding: 2%; font-family: Arial, sans-serif;} --> </style> <script type="text/javascript"> function sumitup(){ var xax = [document.forms.vote.appr1c, document.forms.vote.appr2c, document.forms.vote.appr3c, document.forms.vote.appr4c]; var sum, grp, x, y; var acc = Array(3); sum = 0; for(x = 0; x < 3; x++){ acc[x] = 0; for(y = 0; y < xax.length; y++){ grp = xax[y]; if(grp[x].checked){ sum = sum + 1; acc[x] = acc[x] + 1; }; }; }; alert('A: ' + acc[0] + ', B: ' + acc[1] + ', C: ' + acc[2] + ', TOTAL: ' + sum); }; </script> </head> <body> <form name="vote" id="vote" action="none"> <p><fieldset> <legend>Block 1</legend> <input type="radio" name="appr1c" value="yes" />A <input type="radio" name="appr1c" value="yes" />B <input type="radio" name="appr1c" value="yes" />C </fieldset></p> <p><fieldset> <legend>Block 2</legend> <input type="radio" name="appr2c" value="yes" />A <input type="radio" name="appr2c" value="yes" />B <input type="radio" name="appr2c" value="yes" />C </fieldset></p> <p><fieldset> <legend>Block 2</legend> <input type="radio" name="appr3c" value="yes" />A <input type="radio" name="appr3c" value="yes" />B <input type="radio" name="appr3c" value="yes" />C </fieldset></p> <p><fieldset> <legend>Block 2</legend> <input type="radio" name="appr4c" value="yes" />A <input type="radio" name="appr4c" value="yes" />B <input type="radio" name="appr4c" value="yes" />C </fieldset></p> <input type="button" id="startx" name="startx" value="GO" onclick="javascript:sumitup()" /> </form> </body> </html>
Last edited by MidiMagic; May 8th, 2007 at 3:00 pm.
Daylight-saving time uses more gasoline
![]() |
Similar Threads
- Help using looping in JS (JavaScript / DHTML / AJAX)
- Little confusion in multiple checkboxes (JavaScript / DHTML / AJAX)
- Arrrgggghhhh... Radio Buttons are the bain of my life! Can someone please help. (ASP)
- dynamically generated textboxes & radio buttons php, insert into db (PHP)
- Javascript Arrays (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Frames
- Next Thread: JS generated page 'save as' issues
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array beta browser captchaformproblem cart checkbox child class close codes column css date debugger decimal dependent design disablefirebug dom download editor element embed engine enter error events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe index internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl listbox maps masterpage math media menu mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post problem programming prototype redirect runtime safari scale scriptlets scroll search security select session shopping size software toggle unicode variables w3c web wysiwyg \n






