Check / Uncheck All with Javascript and Prototype

Tagged with Prototype Javascript

Language: HTML / XHTML

View as text

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  <title>Test</title>
   <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js">
   </script> 
 </head>
 <body id="mypage">
   
   <input id="checkbox_selector" type="checkbox" value="0" /><label for="checkbox_selector"> Select / Deselect All</label>  <br />
   
   <form id="myform">
     
     <input type="checkbox" value="0" />  <br />
     <input type="checkbox" value="0" />  <br />
     <input type="checkbox" value="0" />  <br />
     <input type="checkbox" value="0" />  <br />
     <input type="checkbox" value="0" />  <br />
  
   </form>
  
  <script type="text/javascript" charset="utf-8">
    
    var checkboxes = $$("#myform input[type=checkbox]");
    var cbControl = $("checkbox_selector");
    
    cbControl.observe("click", function(){
      checkboxes.each(function(box){
        box.checked = cbControl.checked;
      });
    });
    
  </script>
  
 </body>
 
</html>
Original snippet written by Brian Hogan
Last updated at 12:24 PM on Nov 11, 2008 by Brian Hogan

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.