DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Check/Uncheck All Checkboxes Based On One Check Box

10.14.2006
| 84101 views |
  • submit to reddit
        Bassed on http://www.bigbold.com/snippets/posts/show/350
<html>
  <head>
    <script language='JavaScript'>
      checked = false;
      function checkedAll () {
        if (checked == false){checked = true}else{checked = false}
	for (var i = 0; i < document.getElementById('myform').elements.length; i++) {
	  document.getElementById('myform').elements[i].checked = checked;
	}
      }
    </script>
  </head>
  <body>
    <form id="myform">
      <input type="checkbox" name="foo"/>
      <input type="checkbox" name="bar"/>
      <BR>Check all: <input type='checkbox' name='checkall' onclick='checkedAll();'>
    </form>
  </body>
</html>