• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Check All / UnCheck All

 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic