check all button

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

check all button

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

I used a new logic to it and it worked thank you very much.
Post Reply