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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox help

Status
Not open for further replies.

nicasa

Programmer
Feb 3, 2003
54
0
0
ES
HI There,

I have a simple checkbox called 'checkall' ....

<input name = "checkall" type = "checkbox" onclick = "checkAll()" />

On clicking this checkbox I wish to set ALL of the following checkboxes as 'on'
by invoking "checkAll()".

<input name = "check[]" type = "checkbox" value = "'value1'"/>
<input name = "check[]" type = "checkbox" value = "'value2'"/>
<input name = "check[]" type = "checkbox" value = "'value3'"/>

I NEED to use arrays as shown above,

Any help appreciated,

regards,

Steven M



 
Steven, just use something like this:

Code:
function Checkall(){
var Checkboxes = document.getElementsByTagName("input")

for (var i=0;i<=Checkboxes.length-1;i++){
if(Checkboxes[i].type=="checkbox"){
Checkboxes[i].checked = true;
}
}
}

Basically this loops through all "input" tags and then then has a look at the type to see if it's a checkbox, if so then it check's it!

Cheers

Nick
 
Here's another version...

Code:
<input name="checkall" type="checkbox" onclick="checkAll([red]this[/red])" />

<script language="JavaScript">
function checkAll(c){
  var chks = document.getElementsBy[red]Name[/red]("check[]");
  for(i=0;i<chks.length;i++)
    chks[i].checked = c.checked;
}
</script>

Adam
 
THanks guys,

That is very helpful,

regards,

Steven M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top