File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Check All / UnCheck All

Jessica Sant

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++){
2. Otherwise you can do:
function setChecked(isChecked){
var arr = document.form.elements;
for(var i=0;i<arr.length;i++){

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

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

I agree. Here's the link:
subject: Check All / UnCheck All
It's not a secret anymore!