Our blog

 

Javascript Toggle Checkboxes

I seem to use this little snippet loads so I thought I would post it up for safe keeping:

Toggling all checkboxes on a page:

CODE:
  1. <script type="text/javascript">
  2. function toggleCheckboxes() {
  3. // written by Daniel P 3/21/07
  4. // toggle all checkboxes found on the page
  5.     var inputlist = document.getElementsByTagName("input");
  6.     for (i = 0; i <inputlist.length; i++) {
  7.     if ( inputlist[i].getAttribute("type") == 'checkbox' ) { // look only at input elements that are checkboxes
  8.             if (inputlist[i].checked)   inputlist[i].checked = false
  9.             else                                inputlist[i].checked = true;
  10.         }
  11.     }
  12. }
  13. </script>
  14. <button onClick="toggleCheckboxes();">Toggle Enabled Fields</button>

I can't remember where I first found it. If you know where it originates please feel free to add a link in the comments

More Reading:

One Comments

Joe Mamma
July 23rd, 2010

I don't know if you were first since you don't date your blog but I searched based on togglecheckboxes and Daniel P and found the only other link on the GOOG with the same signature: http://snipplr.com/view/2407/toggle-all-form-checkboxes/

 

 

Leave a Reply