Page 1 of 1

check all button

Posted: Wed Mar 31, 2004 3:51 pm
by sulen
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.

Posted: Wed Mar 31, 2004 8:27 pm
by tim
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

Posted: Thu Apr 01, 2004 1:40 am
by sulen
I used a new logic to it and it worked thank you very much.