Cheking a group of checkboxes based on a master checkbox

J

Jeff Robichaud

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.type == "checkbox")
{
arrayInput.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 ?
 
L

Larry Bud

Jeff said:
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.type == "checkbox")
{
arrayInput.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 ?


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.type == "checkbox" &&
arrayInput.name.substring(0,4)=="can_")
.....
 
R

RobB

Jeff said:
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.type == "checkbox")
{
arrayInput.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 ?


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top