Connecting Tech Pros Worldwide Help | Site Map

Cheking a group of checkboxes based on a master checkbox

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 05:35 PM
Jeff Robichaud
Guest
 
Posts: n/a
Default Cheking a group of checkboxes based on a master checkbox

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 ?



  #2  
Old July 23rd, 2005, 05:35 PM
Larry Bud
Guest
 
Posts: n/a
Default 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_")
.....

  #3  
Old July 23rd, 2005, 05:35 PM
RobB
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.