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. |