Javascript Toggle Checkboxes

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

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:

<script type="text/javascript">
function toggleCheckboxes() {
// written by Daniel P 3/21/07
// toggle all checkboxes found on the page
  	var inputlist = document.getElementsByTagName("input");
  	for (i = 0; i < inputlist.length; i++) {
   	if ( inputlist[i].getAttribute("type") == 'checkbox' ) {	// look only at input elements that are checkboxes
			if (inputlist[i].checked)	inputlist[i].checked = false
			else								inputlist[i].checked = true;
<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

Tags: edmondscommerce