Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check All / UnCheck All

 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jess here is what I talked about to you using the option 1 adusted to your needs plus another feature you may like.

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic