Posted By

krisdb on 04/11/07


Tagged

javascript


Versions (?)

Who likes this?

12 people have marked this snippet as a favorite

jonhenshaw
luman
Hirmine
vali29
SpinZ
f0vela
leitmotiv
buscarini
palimadra
seanpowell
ljayz
wirenaught


select/deselect all checkboxes


 / Published in: JavaScript
 

  1. //checks all checkboxes
  2. function checkAll(field) {
  3. if (!(field.length)) {
  4. field.checked = true;
  5. }
  6. else {
  7. for (var i = 0; i < field.length; i++) {
  8. field[i].checked = true;
  9. }
  10. }
  11. }
  12.  
  13. //unchecks all checkboxes
  14. function uncheckAll(field) {
  15. if (!(field.length)) {
  16. field.checked = false;
  17. }
  18. else {
  19. for (var i = 0; i < field.length; i++) {
  20. field[i].checked = false;
  21. }
  22. }
  23. }
  24.  
  25. <a href="javascript:checkAll(document.FRM.FIELD);">select</a> / <a href="javascript:uncheckAll(document.FRM.FIELD);">deselect</a> all
  26.  
  27. loop through all checkboxes in a form:
  28.  
  29. function checkAll() {
  30. for (i=0; i<document.FormName.elements.length; i++) {
  31. if (document.FormName.elements[i].type=="checkbox")
  32. document.FormName.elements[i].checked=true;
  33. }
  34. }

Report this snippet  

You need to login to post a comment.