Jump to content

Select All Checkboxes


Dysan

Recommended Posts

You can use this..

I use this in my work projects.

 

 

If you want to select specific fields..

I used an array type of input name.

HTML Form:

<form name="test">
<input type="checkbox" id="checkall" value="" onclick="toggleCheck('chk[]', this.checked);" /> Check All<br />
<input type="checkbox" name="chk[]" value="1" /> Check 1 <br />
<input type="checkbox" name="chk[]" value="2" /> Check 2 <br />
<input type="checkbox" name="chk[]" value="3" /> Check 3 <br />
<input type="checkbox" name="chk[]" value="4" /> Check 4 <br />
<input type="checkbox" name="chk[]" value="5" /> Check 5 <br />
</form>

 

Javascript Function:

function toggleCheck(name, value){
var inputs = document.getElementsByTagName("input");
for(var i=0; i<inputs.length; i++){
	if((inputs[i].type=="checkbox") && (inputs[i].name==name))
		inputs[i].checked = value;
}
}

 

if you want to select all checkboxes...

 

HTML Form:

<form name="test">
<input type="checkbox" id="checkall" value="" onclick="toggleCheck(this.checked);" /> Check All<br />
<input type="checkbox" name="chk1" value="1" /> Check 1 <br />
<input type="checkbox" name="chk2" value="2" /> Check 2 <br />
<input type="checkbox" name="chk3" value="3" /> Check 3 <br />
<input type="checkbox" name="chk4" value="4" /> Check 4 <br />
<input type="checkbox" name="chk5" value="5" /> Check 5 <br />
</form>

 

Javascript Function:

function toggleCheck(value){
var inputs = document.getElementsByTagName("input");
for(var i=0; i<inputs.length; i++){
	if(inputs[i].type=="checkbox")
		inputs[i].checked = value;
}
}

 

Link to comment
https://forums.phpfreaks.com/topic/83303-select-all-checkboxes/#findComment-424008
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.