Kevin A. Freitas

E: freitaka@blarg.net
P: 253.229.5093
W: kevinfreitas.net/pro

Checking multiple boxes with just one stroke

By Kevin A. Freitas

Many websites now utilize forms and checkboxes to allow users to control amazing amounts of information. Whether choosing the features you desire in a new car or nuking armies of spam in your email, checkboxes were probably there. You also probably noticed some option labeled "check all" on these sites and when you clicked it, hazah, your tendons were saved from the strain of many clicks and, like magic, all the related checkboxes were now filled in. This tutorial will show you how to group checkboxes together and toggle their checked/unchecked state via the DOM.

First, we'll set up the structure of our form and checkboxes. The following code is a simple HTML form with 8 options listed. I've grouped the options using the "id" attribute so we can get at them separately.

<form name="vehicle_info">
  <p>
  <input type="checkbox" name="options" id="exterior" value="alloy wheels" /> Alloy wheels<br />
  <input type="checkbox" name="options" id="exterior" value="sunroof" /> Sunroof<br />
  <input type="checkbox" name="options" id="exterior" value="spoiler" /> Spoiler<br />
  <input type="checkbox" name="options" id="exterior" value="racing stripes" /> Racing Stripes<br />
  </p>

  <p>
  <input type="checkbox" name="options" id="interior" value="leather" /> Leather<br />
  <input type="checkbox" name="options" id="interior" value="heated seats" /> Heated Seats<br />
  <input type="checkbox" name="options" id="interior" value="cup holder" /> Cup Holder<br />
  <input type="checkbox" name="options" id="interior" value="bowling lane" /> Bowling Lane<br />
  </p>
</form>

Great. Now we need some way of altering the state of those options. For this we go to some simple DOM-endowed JavaScript.

<script type="text/javascript">
function checkAll(checkWhat) {
  // Find all the checkboxes...
  var inputs = document.getElementsByTagName("input");

  // Loop through all form elements (input tags)
  for(index = 0; index < inputs.length; index++)
  {
    // ...if it's the type of checkbox we're looking for, toggle its checked status
    if(inputs[index].id == checkWhat)
      if(inputs[index].checked == 0)
      {
        inputs[index].checked = 1;
      }
      else if(inputs[index].checked == 1)
      {
        inputs[index].checked = 0;
      }
  }
}
</script>

The function "checkAll" takes one value "checkWhat" that should be used to identify the "id" of the checkboxes you'd like to control. Now you can create a link or two that will do all that hard checking work for you.

<p>
Choose all
<a href="javascript: void(null);" onclick="checkAll('exterior')">Exterior</a> |
<a href="javascript: void(null);" onclick="checkAll('interior')">Interior</a>
</p>

Although we've added a bit of code to help identify groups of checkboxes we've created an incredible amount of flexibility in the varied control we can now assert over our checkboxes. View sample »

Methods gone by

The old way of checking a group of boxes uses the "name" attribute. I've avoided this method because, in this example, I want the actual post information from my checkboxes to all be grouped together. If I wanted the same flexibility to select portions of my checkboxes using the "name" attribute I would now need to compile different sets of data once the form was submitted. Using the DOM and the "id" attribute keeps the form's post information grouped together while allowing the user to have access to selecting portions of the boxes.

(2004-09-15)

Comments (7) | To Top


1/6/2005 @ 2:59am

Respected Sir,
Greeting from Prabhu
Thanks for ur combobox example.
It's very useful for me
Thanks a lot
Regards
Prabhu

by Prabhu


11/7/2005 @ 6:55am

Good stuff. I wonder if it's possible to make the link text dynamic, for example so it changes between check all and uncheck all when you click it.

by Dan Bailey


11/7/2005 @ 7:03am

You could do that by adding an onclick event to the "check/uncheck" link itself and simply replace the text once it's clicked. The only reason against doing that would be that if someone clicked something saying "Check All" they may then go uncheck everything themselves, leaving an "Uncheck All" option pointless. To make that work completely logically you'd have to have event listeners on every checkbox so, in the above case, the "Uncheck" text would change back if nothing were checked. I say for simplicity to keep them separate.

Cheers! ~ Kevin

by KevinFreitas


12/7/2005 @ 11:46am

I'm sorry to say that your example is deeply flawed. You define several identical IDs. This is not accepted in any flavor of HTML.

by Sébastien


12/7/2005 @ 1:11pm

Sébastien: You're very right. I wrote this article before I had a full understanding of all things DOM. I'll do a simple re-write that will instead put the IDs in the <p> tag.

Thanks for the push. Cheers! ~ Kevin

by KevinFreitas


12/7/2005 @ 7:37pm

I'm not at all a DOM/JavaScript pro myself but I know a little about HTML and validation.

Your solution should work fine; you could also use the fact that name attributes need not be unique.

cheers

by Sébastien


12/8/2005 @ 8:21am

Very true. Thanks for the nudge anyway. Cheers! ~ Kevin

by KevinFreitas

Post Comments


(No HTML, comments with http://... will be reviewed)

Remember me? Yes   No

Please do not fill in the following field: