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.
If you found that my code was helpful in any way, shape, or form and would like to buy me a beer, please use the Donate button below =) Cheers!
this is just what I was looking for, thank you for this, Allen
This helped me figure out how to select all checkboxes(by type), not all inputs(including radio inputs)!!
thanks!!!
No problem =) I’m glad it helped.
Thanks a lot for this post on the Javascript..It has kept me up for 3straight nites and now here it is,the solution to my worries!! God bless you plenty!!
Hi Samuel,
I’m glad you found the post useful! =)
Allen
This is great, however what I’m looking for is an alert that says, You need to select a choice in the candy section if there is none chosen on submit.
Can you help?
Hi Gretchen,
The code to do this would be pretty ugly compared to what jQuery is capable of:
http://stackoverflow.com/questions/4086957/jquery-see-if-any-or-no-checkboxes-are-selected
Let me know if this is not an option for you.
Allen
Thanks mate, very useful
very good example
neatly explained….
thanks a lot
This was a great demo. Exactly what I was looking for.
This content is very helpful
Your script is just perfect. Exactly what I was looking for.
Thank you heaps.
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….
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
Thx a lot man!
This example was just what i needed :)
Exactly what i needed cheers mate