Reputation: 809
I want to return the index of a given tr when I click on an element inside of a td.
The html
<table class="tablesorter-js">
<thead>
<tr>
<th class="txt-align-left>Builder</th>
<th class=" txt-align-left " jobs">Current jobs</th>
<tbody>
<tr>
<td>some text <a class="link" href="#">A link</a>
</td>
<td>tome more text</td>
</tr>
<tr>
<td>some text <a class="link" href="#">A link</a>
</td>
<td>tome more text</td>
</tr>
</tbody>
</table>
the javascript
$('.link').click(function(e {
e.preventDefault();
console.log(oTable.fnGetPosition(this))
});
I know I can invoke the index of the current tr with this
var oTable = $('.tablesorter-js').dataTable();
$('.tablesorter-js tbody tr').click( function () {
var pos = oTable.fnGetPosition(this)
// I want the position of the tr element when I click on .link
return pos;
});
How do I get that index of the current tr element when I click on .link?
Upvotes: 1
Views: 854
Reputation: 790
$('.link').click(function(e) {
e.preventDefault();
console.log( $(this).closest('tr').index() );
});
Upvotes: 1
Reputation: 36127
Use index() jQuery function
var oTable = $('.tablesorter-js').dataTable();
$('.tablesorter-js tbody tr').click( function () {
//var pos = oTable.fnGetPosition(this)
var idx = $(this).index(".tablesorter-js tbody tr");
return idx;
});
$('.link').click(function(e) {
e.preventDefault();
console.log( $(this).closest('tr').index(".tablesorter-js tbody tr") );
});
For more details .index() JQuery
Upvotes: 0
Reputation: 2577
You can get index of current <tr>
element whose link is clicked with below jQuery code:
$('.link').click(function(){
alert($(this).closest('tr').index());
});
Upvotes: 0