[Javascript] select all checkboxes script
Cutter
java.script at seacrets.com
Fri Feb 3 04:56:38 CST 2006
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) />
Michael Borchers wrote:
> i need a little skript to check or uncheck all checkboxes on button click.
>
> the form name and / or the checkbox name should be parsed dynamically
> to the function
>
> function checkAll(formName, checkboxName)
>
> i tried to modify this function:
> function CheckAll()
> {
> if(document.form.ALL)
> {
> var c = document.form.ALL.checked;
> }
> for (var i=0;i<document.form.elements.length;i++)
> {
> var e = document.form.elements[i];
>
> if(e.name != 'ALL')
> {
> if((e.checked!=c) && (e.type=="checkbox"))
> {
> e.click();
> e.checked = c;
> }
> }
> }
> }
> but how can i parse the forms name?
> how about modifying the function without using a form name,
> only using the checkboxes name?
>
> thanks
> ------------------------------------------------------------------------
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
More information about the Javascript
mailing list