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>

Related Posts

  • Tutorial Examples
  • Getting Ready for Web 2.0 Effects with Scriptaculous
  • Error Output With Scriptaculous
  • Ryboe Tag Cloud V.1.01 Released
  • Ryboe Tag Cloud Plugin for WordPress
  • 8 Responses to “Select All Checkboxes with Prototype JS”

    1. Nick Says:

      Thanks for that.
      Really helpful

    2. Steev Says:

      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

    3. psyke Says:

      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

    4. Hora_ce Says:

      Hi all,

      this is what I would use

      allCheckboxes = $$('input[type=checkbox]');

      and regarding checkboxes within a contaner…

      containerCheckboxes = $(container).select('input[type=checkbox]')

    5. psyke Says:

      thank you !

    6. SuShi Says:

      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.

    7. checkboxes is not defined Says:

      I'm getting
      checkboxes is not defined

      please help

    8. Programmer Says:

      Thanks a lot ! You saved my time ! :D

    Leave a Reply