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
Post a Comment