javascript - jQuery: Determine from which <tr> the function is called -


i have knockout grid looks (nothing special):

knockoutjs grid editable

when click edit, must determine in <tr> called. example click edit on first user, must log in console "1", , on.

my table's code:

<table class="table table-striped table-bordered table-condensed" data-bind='visible: users().length > 0'>     <thead>         <tr>             <th>userid</th>             <th>username</th>             <th>firstname</th>             <th>lastname</th>             <th>email</th>             <th>date</th>             <th />         </tr>     </thead>     <tbody data-bind='foreach: users'>         <tr>             <td>                 <label data-bind="text: userid" />             </td>             <td>                 <input data-bind='value: username' />                 <label data-bind="text: username" />             </td>             <td>                 <input data-bind='value: firstname' />                 <label data-bind="text: firstname" />             </td>             <td>                 <input data-bind='value: lastname' />                 <label data-bind="text: lastname" />             </td>             <td>                 <input data-bind='value: email' />                 <label data-bind="text: email" />             </td>             <td>                 <input data-bind='value: date' />                 <label data-bind="text: date" />             </td>             <td class="tools">                 <a class="apply" href="#" data-bind="click: $root.applyuser">apply</a>                 <a class="edit" href="#" data-bind="click: $root.edituser">edit</a>                 <a class="update" href="#" data-bind='click: $root.updateuser'>update</a>                 <a class="delete" href='#' data-bind='click: $root.removeuser'>delete</a>             </td>         </tr>     </tbody> </table> 

this attempt:

$('a.edit').click(function () {                     var rowindex = $(this).parent()                                           .parent()                                           .children()                                           .index($(this).parent());                     console.log('row: ' + rowindex);                 }); 

what missing?

you can rather use .closest() traversing closest tr parent.

if tr elements not siblings of each other: pass object method .index() on tr collection:

 $('tr').index($(this).closest('tr')); 

if tr elements siblings of each other:

 $(this).closest("tr").index() 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -