Select All Checkboxes with Prototype JS
If you are a looking for an easy way to select all checkboxes on a page, there are a number of ways you can quickly accomplish this with the Prototype JS library.
Let's say your HTML looks like this:
<form id="options">
<fieldset><input type="text" value="test"></fieldset>
<fieldset><input type="checkbox" value=0> 0</fieldset>
<fieldset><input type="checkbox" value=1> 1</fieldset>
<fieldset><input type="checkbox" value=2> 2</fieldset>
<fieldset><input type="checkbox" value=3> 3</fieldset>
</form>
It's fairly straight forward to grab all of the input elements of type "checkbox" without also getting the element of type "text".
In both cases we will store our results as objects in an array, named checkboxes.
var checkboxes = [];
Now that we have this array ready for our objects, let's select them.
The first method, uses the $$ operator to select all inputs on the page. Then we use the each() method to iterate through each input element and check the type. If our type is equal to the string 'checkbox', store it to our array checkboxes.
checkboxes = $$('input').each(function(e){ if(e.type == 'checkbox') checkboxes.push(e) });
The second method, uses the hand Form object method called getInputs(). To use this method, first we need to select the form object. In this example, we do so by used the $ operator and the id attribute of the form. Once we have that object, we call the getInputs() method and pass it the argument 'checkbox', which will define the input type we are seeking.
var form = $('options');
checkboxes = form.getInputs('checkbox');
In both of these cases, checkboxes will be equal to:
checkboxes // ---> returns "[input 0, input 1, input 2, input 3]"
If you would like to check all of these checkboxes, you can do so with this line of code:
checkboxes.each(function(e){ e.checked = 0 });
You can also assign it to a link with this HTML:
<a href="#" onclick="checkboxes.each(function(e){ e.checked = 0 });">Select All</a>


August 19th, 2008 at 5:36 am
Thanks for that.
Really helpful
April 23rd, 2009 at 9:06 am
This line, " checkboxes.each(function(e){ e.checked = 0 });"
should read, " checkboxes.each(function(e){ e.checked = 1 });"
"e.checked = 1" to Select all
"e.checked = 0" to Unselect all
May 26th, 2009 at 11:02 am
hello
what about checking/unckecking checkboxes in a container ?
example: check only the checkboxes in a specific div container (or table) ?
thanks for your answer, and great job
June 3rd, 2009 at 7:45 am
Hi all,
this is what I would use
allCheckboxes = $$('input[type=checkbox]');
and regarding checkboxes within a contaner…
containerCheckboxes = $(container).select('input[type=checkbox]')
June 5th, 2009 at 6:44 am
thank you !
July 13th, 2009 at 4:36 pm
I need to check if at least one check box is checked in an array of check boxes before submitting the form. Please give me the code.
August 31st, 2009 at 1:51 am
I'm getting
checkboxes is not defined
please help
November 19th, 2009 at 7:33 am
Thanks a lot ! You saved my time !