[Javascript] select all checkboxes script

Michael Borchers list at tridemail.de
Tue Feb 7 08:41:04 CST 2006


----- Original Message ----- 
From: "Cutter" <java.script at seacrets.com>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Friday, February 03, 2006 11:56 AM
Subject: Re: [Javascript] select all checkboxes script


> Here's something I recently contributed to a project a friend of mine 
> gives away...
>
> Hope it helps,
> Cutter
>
> <!---
> //    Author:        Steve 'Cutter' Blades
> //    Revision:    Add js scripting for a 'Select All' type checkbox
> //                Aside from the below javascript the following line are 
> required:
> //                on the 'Select All' checkbox input element you will need 
> and
> //                onclick event of 
> 'toggleCheckboxes(this,document.forms.#formname#.#checkfields#)',
> //                and on the checkbox fields (all of the same id/name) you 
> will need
> //                an onclick event of 
> 'uncheckSelectAllBox(document.forms.#formname#.#selectallfieldname#,this)
> --->
> <script language="javascript" type="text/javascript">
>    // Functions for checking and unchecking all checkboxes passed by name
>    function toggleCheckboxes(checkItFldObj, fldObj){
>        if (checkItFldObj.checked){
>            checkAll(fldObj);
>        } else {
>            uncheckAll(fldObj);
>        }
>    }
>   // Function to check all fields
>    function checkAll(field){
>        if (!field.length){
>            field.checked = true;
>        } else {
>            for (i = 0; i < field.length; i++)
>                field[i].checked = true ;
>        }
>    }
>   // Function to uncheck all fields
>    function uncheckAll(field){
>        //alert(field.length);
>        if (!field.length){
>            field.checked = false;
>        } else {
>            for (i = 0; i < field.length; i++)
>                field[i].checked = false ;
>        }
>    }
>   // Function to uncheck the 'Select All' checkbox if an item is unchecked
>    function uncheckSelectAllBox(checkItFldObj,fldObj){
>        if ((fldObj.checked == false) && (checkItFldObj.checked == true))
>            checkItFldObj.checked = false;
>    }
> </script>
>
> All<br />
> <input type="checkbox" name="selectAll" id="selectAll" 
> onclick="toggleCheckboxes(this,document.forms.myFormName.chkbxFldName);" 
> />
>
> <!--- Cutter 01.25.06: Add onclick function call to uncheck 'Select All' 
> box --->
> <input type="checkbox" name="chkbxFldName" id="chkbxFldName" value="" 
> onClick="uncheckSelectAllBox(document.forms.myFormName.selectAll,this) />
>

the script works fine,
but not if the name/id is

chkbxFldName[0], chkbxFldName[1], chkbxFldName[2] etc...!? :( 




More information about the Javascript mailing list