Select All Checkboxes Based On Class


/ Published in: JavaScript

This is a simple javascript snippet that will allow you to specify a particular class to a group of checkboxes and then select them all at once when you click the master checkbox. This came in very handy for a store inventory script when I needed to select all of the models of a particular manufacturer.


Copy this code and paste it in your HTML
  1. <script language="JavaScript">
  2. function checkAll(theForm, cName, status) {
  3. for (i=0,n=theForm.elements.length;i<n;i++)
  4. if (theForm.elements[i].className.indexOf(cName) !=-1) {
  5. theForm.elements[i].checked = status;
  6. }
  7. }
  8. </script>
  9.  
  10. <form id="selectForm">
  11.  
  12. Set 1:
  13. <br>
  14. <input type="checkbox" name="selected" value="01" class="results1"><br>
  15. <input type="checkbox" name="selected" value="02" class="results1"><br>
  16. Select All/None <input type="checkbox" onclick="checkAll(document.getElementById('selectForm'), 'results1', this.checked);" />
  17. <br>
  18. <br>
  19.  
  20. Set 2:
  21. <br>
  22. <input type="checkbox" name="selected" value="01" class="results2"><br>
  23. <input type="checkbox" name="selected" value="02" class="results2"><br>
  24. Select All/None <input type="checkbox" onclick="checkAll(document.getElementById('selectForm'), 'results2', this.checked);" />
  25.  
  26. </form>

URL: http://www.customcmssolutions.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.