Check uncheck all checkboxes javascript

Hi friends. To check or uncheck form check boxes, use this javascript function.
Its jQuery version is available in another article named Check uncheck all checkboxes with jQuery

The Html Form [ For static contents ]
 <script type="text/javascript">
  function checkAll(formName, status){
   for (i = 0; i < formName.length; i++)
   formName[i].checked = status.checked? true:false
   }
 </script>
  
 <form name ="checkForm">
  <input type="checkbox" name="all" 
      onClick="checkAll(this.form,this)">Check/Uncheck All
  <input type="checkbox" name="one" value ="one">One
  <input type="checkbox" name="two" value ="two">Two
  <input type="checkbox" name="three" value ="three">Three
  <input type="checkbox" name="four" value ="four">Four
  <input type="checkbox" name="five" value ="five">Five
 </form>


The Php Form [ For Dynamic Contents ]
 <script type="text/javascript">
  function checkAll(formName, status){
   for (i = 0; i < formName.length; i++)
   formName[i].checked = status.checked? true:false
   }
 </script>
 
 <form name ="checkForm">
  <input type="checkbox" name="all" 
        onClick="checkAll(this.form,this)">Check/Uncheck All
  <?php
  mysql_connect('localhost','username','password');
  mysql_select_db('databaseName');
  
  $sql="select id,name from tableName";
  $res=mysql_query($sql);
  while($row=mysql_fetch_array($res)){
   echo '<input type="checkbox" name="chk[]" 
       value ='.$row["id"].'>'.$row["name"].'<br>';
  }
  ?>
 </form>


The Example

Check/Uncheck All
One
Two
Three
Four
Five
Check uncheck all checkboxes javascript Check uncheck all checkboxes javascript Reviewed by JS Pixels on April 29, 2011 Rating: 5

2 comments:

  1. would you please tell how to checkall the checkboxes when it varies in number based on the result from the database.

    ReplyDelete
  2. You will have to use a loop for this purpose. I have updated the form for dynamic contents. Look above "The Php Form [ For Dynamic contents ]"

    ReplyDelete

Altaf Web. Powered by Blogger.