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:

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!

Published by

Allen Liu

Building websites is a passion of mine. During the course of my work, I have created some snippets of code that I hope will prove useful to other developers.
View all posts by Allen Liu

20 thoughts on “How to loop through checkboxes or radio button groups via JavaScript”

Hi Allen,
This example really helped. However, I am trying to find a complete Javascript code example where one submit button will display the output for several checkbox groups. I am mostly having trouble with the coding for the functions and having them work the way they should to drive the various check box groups.
Here is the code I have so far, hope you can help:

Thanks Allan,
The code you sent over worked great. I do have one last question for you as it relates to a project that I am currently working on. Using your initial above form example a model, how would you recode it to consist of all checkboxes (in place of the 2 radio button options)? For instance, say you wanted the output to display as follows:

programming languages selected:
food selected:
drinks selected:

I have been trying to come up with solutions, but have not succeeded. Your valuable assistance in this would be greatly appreciated.
Thanks again,
Hugo

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.

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