I am trying to create a "check all" and "uncheck all buttons" for checkboxes on a php page on my site. I know how to write the code for it when the checkboxes are static but I am confused how to do it when the checkboxes are dynamic. Here is the code
<script type="text/javascript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field.checked = false; }
checkflag = "false";
return "Check All"; }
}
</script>
The HTML code for the checkbox is :
<input type="checkbox" name="mail[]" value="<?=$cemail?>">
For the buttons are :
<input type=button value="Check All" onClick="this.value=check(this.form.list)">
I am getting a javascript error. Please any help will be appreciated.
check all button
Moderator: General Moderators
Whats your JS error?
I always used:
function chall(field) {
for (i = 0; i < field.length; i++)
field.checked = true;
}
function uchall(field) {
for (i = 0; i < field.length; i++)
field.checked = false;
}
// here the button to check all the items.
<input type=button value=/"check all/"
onClick=/"chall(document.formname.list)/">
works for me, change formname to whatever the name of your form is
I always used:
function chall(field) {
for (i = 0; i < field.length; i++)
field.checked = true;
}
function uchall(field) {
for (i = 0; i < field.length; i++)
field.checked = false;
}
// here the button to check all the items.
<input type=button value=/"check all/"
onClick=/"chall(document.formname.list)/">
works for me, change formname to whatever the name of your form is