Posted By

fauverism on 01/08/11


Tagged

javascript function


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jillella


Check or Uncheck all Checkboxes


 / Published in: JavaScript
 

URL: http://www.somacon.com/p117.php

  1. function SetAllCheckBoxes(FormName, FieldName, CheckValue)
  2. {
  3. if(!document.forms[FormName])
  4. return;
  5. var objCheckBoxes = document.forms[FormName].elements[FieldName];
  6. if(!objCheckBoxes)
  7. return;
  8. var countCheckBoxes = objCheckBoxes.length;
  9. if(!countCheckBoxes)
  10. objCheckBoxes.checked = CheckValue;
  11. else
  12. // set the check value for all check boxes
  13. for(var i = 0; i < countCheckBoxes; i++)
  14. objCheckBoxes[i].checked = CheckValue;
  15. }
  16.  
  17. <form method="GET" action="page17.php" name="myForm" onsubmit="return false;">
  18. <label for="myCheckbox1">
  19. <input type="checkbox" name="myCheckbox" value="1" id="myCheckbox1">
  20. I like Britney Spears </label>
  21. <br>
  22. <label for="myCheckbox2"><input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
  23. I like Hillary Duff </label>
  24. <br>
  25. <label for="myCheckbox3"><input type="checkbox" name="myCheckbox" value="3" id="myCheckbox3">
  26. I like Mandy Moore </label>
  27. <br><input type="button" onclick="SetAllCheckBoxes('myForm', 'myCheckbox', true);" value="I like them all!">
  28. &nbsp;&nbsp;
  29. <input type="button" onclick="SetAllCheckBoxes('myForm', 'myCheckbox', false);" value="I don't like any of them!">
  30. </form>

Report this snippet  

You need to login to post a comment.