Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check all/uncheck all checkboxes

Status
Not open for further replies.

mufka

ISP
Dec 18, 2000
587
US
I found a nice example of a javascript that allows you to check or uncheck all of the checkboxes in a form. I've hit a roadblock in my implementation because I need each checkbox to have a different name or be part of an array (preferably the former).

I've tried putting the checkbox name in [] like this:
Code:
<input type="button" name="Check_All" value="Check All" onClick="Check(document.testform.["check_box[]"])">
But that does not work for me.

Any assistance would be appreciated.

Here is the full test code that I'm using:
Code:
<?	
echo "<SCRIPT LANGUAGE=\"JavaScript\">";
echo "function Check(chk)";
echo "{";
echo "if(document.testform.Check_All.value==\"Check All\"){";
echo "for (i = 0; i < chk.length; i++)";
echo "chk[i].checked = true ;";
echo "document.testform.Check_All.value=\"UnCheck All\";";
echo "}else{";
echo "for (i = 0; i < chk.length; i++)";
echo "chk[i].checked = false ;";
echo "document.testform.Check_All.value=\"Check All\";";
echo "}";
echo "}";
echo "</script>";
echo "<form name='testform' action='' method='post'>";
echo "<input type='checkbox' name='check_box[]' value='1'>1<br>";
echo "<input type='checkbox' name='check_box[]' value='2'>2<br>";
echo "<input type='checkbox' name='check_box[]' value='3'>3<br>";
echo "<input type='checkbox' name='check_box[]' value='4'>4<br>";
echo "<input type='checkbox' name='check_box[]' value='5'>5<br>";
echo "<input type='button' name='Check_All' value='Check All' onClick='Check(document.testform.['check_box[]'])'>";
echo "</form>";
 
Try
Code:
document.testform.[b][red]elements[/red][/b]["check_box[]"]

Lee
 
You've provided two bits of code, both invalid, as the first nests double quotes and the second nests single quotes.

Apart from letting us have the real code you're using (because it cannot be both), and ensuring we only see the client-side code (not the PHP which is not what the browser sees), try this:

Code:
onClick="Check(document.forms['testform'].elements['check_box[]'])"

Note how I'm not nesting the same type of quote in that code.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
My mistake. I was still messing with the quotes when I wrote my first message. The second code is the full code that I'm trying to get to work.

I got it to work with:
Code:
echo "<input type='button' name='Check_All' value='Check All' onClick='Check(document.testform.elements[\"check_box[]\"])'>";

Is there a way to do it with a different name= for each checkbox?
 
You could loop over the form's 'elements' collection, testing the type attribute of each item to see if it is a checkbox or not.



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top