How to loop through checkboxes or radio button groups via JavaScript
May 15th, 2008 | Published in javascript | 7 Comments
Do you have a form with checkboxes or radio buttons that you would like to loop through via JavaScript? This JavaScript function will do just that!
Here is the plain HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <form name="thisForm"> I like to program in:<p> <input type="checkbox" value="PHP" CHECKED>PHP<p> <input type="checkbox" value="Perl">Perl<p> <input type="checkbox" value="Ruby">Ruby<p> <input type="checkbox" value="ASP">ASP<p> <hr> I like to eat:<p> <input type="radio" value="Snickers" name="candy" CHECKED>Snickers<p> <input type="radio" value="Hershey's" name="candy">Hershey's<p> <input type="radio" value="M&M's" name="candy">M&M's<p> <input type="radio" value="Nerds" name="candy">Nerds<p> <hr> I like to drink:<p> <input type="radio" value="Coke" name="drink" CHECKED>Coke<p> <input type="radio" value="Gatorade" name="drink">Gatorade<p> <input type="radio" value="Pepsi" name="drink">Pepsi<p> <input type="radio" value="Milk" name="drink">Milk<p> <br> <input type="button" value="Submit" onclick="loopForm(document.thisForm);"> </form> <p> <div id="cbResults"></div> <div id="radioResults"></div> |
As usual, there is not too much action going on here. It is just a plain form with some checkboxes and 2 radio button groups called candy and drink. Radio buttons with the same name will be grouped and allows the users to only select one option out of the entire group. The submit button will launch the loopForm JavaScript function and the myForm DOM object is passed over as an argument. There are also 2 divs in lines 23-24 where are checkbox and radio button results will be posted. Now, on to more exciting things.
Here is the JavaScript code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function loopForm(form) { var cbResults = 'Checkboxes: '; var radioResults = 'Radio buttons: '; for (var i = 0; i < form.elements.length; i++ ) { if (form.elements[i].type == 'checkbox') { if (form.elements[i].checked == true) { cbResults += form.elements[i].value + ' '; } } if (form.elements[i].type == 'radio') { if (form.elements[i].checked == true) { radioResults += form.elements[i].value + ' '; } } } document.getElementById("cbResults").innerHTML = cbResults; document.getElementById("radioResults").innerHTML = radioResults; } |
This function loops through all the elements in the form that was passed over to the function.
If the element is of checkbox type and it is checked, the value of this element will be appended to our cbResults variable which is keeping track of our checkbox results.
If the element is of radio type and it is checked, the value of this element will be appended to our radioResults variable which is keeping track of our radio button results.
After all the looping is finished, the results are cbResults and radioResults are inserted into the cbResults div and radioResults div respectively.
Share with a friend:Customize message
September 27th, 2008 at 8:46 am (#)
Exactly what i needed cheers mate
[Reply]
November 4th, 2008 at 1:47 am (#)
Thx a lot man!
This example was just what i needed :)
[Reply]
December 4th, 2008 at 8:16 pm (#)
this is sort of what i wanted. i’m looking to do the same thing but it must change as soon as the checkbox is checked. basicly i’m creating a mail script that displays all the messages in a table and each row gets a checkbox. if you select atleast 1 checkbox then a hidden div (a floating menu) will pop up and say with selected what would you like to do and they would choose delete or mark read or mark unread. any idea on how this can be done
[Reply]
May 16th, 2009 at 5:32 am (#)
Hey,
U gave me such a brilliant idea :)
Actually i’m just learning the JScript and wanna create a test paper where there are 20 questions and some answers. I want to create a script by which i can get the answers ticked by the candidates as well as i want to create a script by which i can find the count of correct answers ticked by them.
your script will hepl me a lot in getting the logic. :)
Thanks a lot….
[Reply]
May 25th, 2009 at 6:04 am (#)
Your script is just perfect. Exactly what I was looking for.
Thank you heaps.
[Reply]
July 30th, 2009 at 6:05 am (#)
This content is very helpful
[Reply]
August 11th, 2009 at 1:18 pm (#)
This was a great demo. Exactly what I was looking for.
[Reply]