Toggle checkboxes


/ Published in: HTML
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2.  
  3. function toggle_checkboxes(id) {
  4. if (!document.getElementById){ return; }
  5. if (!document.getElementsByTagName){ return; }
  6. var inputs = document.getElementById(id).getElementsByTagName("input");
  7. for(var x=0; x < inputs.length; x++) {
  8. if (inputs[x].type == 'checkbox'){
  9. inputs[x].checked = !inputs[x].checked;
  10. }
  11. }
  12. }
  13.  
  14.  
  15. <div id="parent_box">
  16.  
  17. <input type="checkbox" name="foo" value="1" /> 1<br/>
  18. <input type="checkbox" name="foo" value="2" checked="checked" /> 2<br/>
  19. <input type="checkbox" name="foo" value="3" checked="checked" /> 3<br/>
  20.  
  21. <br/>
  22. <input type="button" value="Toggle checkboxes"
  23. onclick="toggle_checkboxes('parent_box')" />
  24.  
  25. </div>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.