[Javascript] select 'nearly' all checkboxes

Triche Osborne wdlists at triche-osborne.com
Thu Feb 16 16:58:06 CST 2006

Michael Borchers wrote:
> now here is the idea:
> the checkboxes belonging to a group are surrounded by a fieldset with an id.
> if i would like to have an "check all boxes of this group" checkbox, is it possible?!

Yes. Include an unnamed checkbox for "all" and attach an onchange event 
that passes its node to a function, something like this (old-style event 
attachment just for illustration):

<input type="checkbox" onchange="checkAll(this);" /> All

In the checkAll function you need to:

(1) Test to be sure the "all" box is checked. (If not, the remaining 
script should not execute. This prevents it firing on an uncheck. Too 
bad the "onselect" event doesn't work.)

(2) If the box was checked, get its parent node, then grab all of the 
child nodes of that parent.

(3) Check the child nodes array for all nodeName == 'INPUT' and set each 
one's checked attribute to true.

(4) For pretty's sake, you can hide the "all" box afterward--or not.

Note: The unnamed 'all' will not be included in the $_POST array. I 
assumed this was the behavior you wanted since including all of the 
checked fields in the array would render it superfluous anyway. If you 
do want to include it, just name it.


More information about the Javascript mailing list