html - Row wise selecting the all checkboxes inside a table with using Jquery -


i have script should check checkboxes inside table row. using below post able check checkboxes inside table. don't want this. want check checkboxes in row wise only.

reference post:

jquery select checkboxes in table

sample html code:

<table> <tr>     <th>         <input type="checkbox" id="selectall" />     </th>     <th>         hi!     </th> </tr> <tr>     <td>         <input type="checkbox" id="1"/>     </td>     <td>         hi!     </td> </tr> <tr>     <td>         <input type="checkbox" id="2"/>     </td>     <td>         hi!     </td> </tr>     </table> 

sample jquery:

  $('#selectall').click(function(e){ var table= $(e.target).closest('table'); $('td input:checkbox',table).prop('checked',this.checked); }); 

see below screenshots project

  1. before clicking check checkbox

enter image description here

  1. after checking check checkbox

enter image description here

looks have duplicate ids select checkboxes. ids must unique. can give same class select checkboxes(say selectall) , use class selector.

also targeting closest tr, can traverse closest tr rather getting table element. , find input checkboxes in them. this:

$('.selectall').click(function(e){   var tr= $(e.target).closest('tr');   $('td input:checkbox',tr).prop('checked',this.checked); }); 

Comments

Popular posts from this blog

android - How to save instance state of selected radiobutton on menu -

python 3 IndexError: list index out of range -

IF statement in MySQL trigger -