Posted By

chemical on 07/28/10


Tagged


Versions (?)

Toggle checkboxes


 / Published in: 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  

You need to login to post a comment.