This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Check All / UnCheck All Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Check All / UnCheck All" Watch "Check All / UnCheck All" New topic
Author

Check All / UnCheck All

Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

I've got a series of checkboxes on a form. I'd like to have 2 "convenience links" where folks can click the "Check All" or "Uncheck All" in order to do just that... kind of like they have on a mail box, like Yahoo.
I did a search in this forum, and couldn't seem to find something like this. Any ideas?
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
Generally speaking, you have to loop through and check/unchek them
The trick there is to get your checkboxes pointers.
1. if all checkboxes to be checked have the same name, you can do
function setChecked(isChecked){
var arr = document.getElementsByName("checkboxesName");
for(var i=0;i<arr.length;i++){
arr[i].checked=isChecked;
}
}
2. Otherwise you can do:
function setChecked(isChecked){
var arr = document.form.elements;
for(var i=0;i<arr.length;i++){
if(isValid(arr[i]))arr[i].checked=isChecked;
}
}

function isValid(element){
return element.type="checkbox";
}
Function isValid could be more complicated, like checking that name starts with some preffix, or check className or id or value - it is all up to you.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Jess here is what I talked about to you using the option 1 adusted to your needs plus another feature you may like.

Eric
 
GeeCON Prague 2014
 
subject: Check All / UnCheck All