php - how to select/deselect all checkboxes generated in loop when parent checkbox is checked -


<script>     $(function(){     $('.parent').on('click',function(){     $(this).find('.child[]').attr('checked',this.checked);     }); }); </script> <table>   <thead>     <tr>       <th>s no.</th>       <th><input type="checkbox" name="select1" id="select1" class="parent" /></th>       <th>title</th>       <th>end date</th>       <th>action</th>       <th>deal type</th>       <th>status</th>     </tr>   </thead>      <tbody>     <tr>     <?php $sql="select * table_name id='$id'";       $result=mysql_query($sql);        $sn=1;       while($show=mysql_fetch_array($result))       {            $student=$show['student'];        ?>       <td><?php  echo $sn; ?></td>       <td><?php echo "<input type='checkbox' name='check[]' class='child[]' id='check[]' value='".$student."'/>"; ?></td>       <td>enddate</td>       <td>view</td>       <td>edit</td>                                                   <td>delete</td>       <td>xyz</td>     </tr>     <?php $sn++ }}?> 

here, checkbox-name=select1 out of loop , parent checkbox. checkbox-name=check array of checkbox coming along data database. if there 10 entries in database, 10 checkboxes come. want if check parent checkbox, checkboxes should checked irrespective of numbers can say, gmail. please tell place javascript/jquery. thank answers.

the this inside click callback refer checkbox itself. $(this).find('.child[]') not find elements.

also note class="index[]" not work jquery, change class="index". can leave name="index[]" is.

if want elements inside scope of parent table (thus allowing have multiple structures of (un)check all), use:

$(function(){   $('.parent').on('click',function(){       $(this).parents('table').find('.child').attr('checked',this.checked);   }); }); 

if want child nodes in document, use:

$(function(){   $('.parent').on('click',function(){       $('.child').attr('checked',this.checked);   }); }); 

kind regards,

arnoud


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -