Connecting Tech Pros Worldwide Help | Site Map

Cheking a group of checkboxes based on a master checkbox

Jeff Robichaud
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi,

This seems like a classic one (but I'm pretty new to Javascript)...

I have a list of checkboxes for all the U.S. states and a checkbox "All" to
check them all at once. The following function performs this:

function CheckAll(masterCheckBox)
{
var arrayInput = document.getElementsByTagName("input");
for(i=0; i<arrayInput.length; i++)
{
if (arrayInput[i].type == "checkbox")
{
arrayInput[i].checked = masterCheckBox.checked;
}
}
}

Ok there's a little overhead because a form will contain a few input
elements that are not of checkbox type but it works (any better idea would
be appreciated).

Now if I want to include another set of checkboxes, say Canadian provinces,
that will also have their checkbox "All". How can I differentiate the two
checkbox sets ?


Larry Bud
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Cheking a group of checkboxes based on a master checkbox



Jeff Robichaud wrote:[color=blue]
> Hi,
>
> This seems like a classic one (but I'm pretty new to Javascript)...
>
> I have a list of checkboxes for all the U.S. states and a checkbox[/color]
"All" to[color=blue]
> check them all at once. The following function performs this:
>
> function CheckAll(masterCheckBox)
> {
> var arrayInput = document.getElementsByTagName("input");
> for(i=0; i<arrayInput.length; i++)
> {
> if (arrayInput[i].type == "checkbox")
> {
> arrayInput[i].checked = masterCheckBox.checked;
> }
> }
> }
>
> Ok there's a little overhead because a form will contain a few input
> elements that are not of checkbox type but it works (any better idea[/color]
would[color=blue]
> be appreciated).
>
> Now if I want to include another set of checkboxes, say Canadian[/color]
provinces,[color=blue]
> that will also have their checkbox "All". How can I differentiate the[/color]
two[color=blue]
> checkbox sets ?[/color]

You need to put a prefix on the names of checkboxes you want to
group.... So Canadian provinces may begin with can_ and you could check
for the name

if (arrayInput[i].type == "checkbox" &&
arrayInput[i].name.substring(0,4)=="can_")
.....

RobB
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Cheking a group of checkboxes based on a master checkbox


Jeff Robichaud wrote:[color=blue]
> Hi,
>
> This seems like a classic one (but I'm pretty new to Javascript)...
>
> I have a list of checkboxes for all the U.S. states and a checkbox[/color]
"All" to[color=blue]
> check them all at once. The following function performs this:
>
> function CheckAll(masterCheckBox)
> {
> var arrayInput = document.getElementsByTagName("input");
> for(i=0; i<arrayInput.length; i++)
> {
> if (arrayInput[i].type == "checkbox")
> {
> arrayInput[i].checked = masterCheckBox.checked;
> }
> }
> }
>
> Ok there's a little overhead because a form will contain a few input
> elements that are not of checkbox type but it works (any better idea[/color]
would[color=blue]
> be appreciated).
>
> Now if I want to include another set of checkboxes, say Canadian[/color]
provinces,[color=blue]
> that will also have their checkbox "All". How can I differentiate the[/color]
two[color=blue]
> checkbox sets ?[/color]

Consider using a button for this. There seems to be an assumption that,
since we're checking checkboxes, yet another cb is the appropriate
control for a 'gang-check'. Actually, using a button separates the
control from the affected fields and is less confusing. Easy to change
its legend too.

Closed Thread