Reputation: 3708
I want to get a row content in my table:
<div class="myTable">
<table id="dataTable">
<tbody>
<tr>
<td>First</td>
</tr>
<tr>
<td>second</td>
</tr>
</tbody>
</table>
</div>
To get the td I use:
$('#dataTable').find('tbody').find('tr:nth-child(1)');
which works and gets the tr data, but when doing it with variable:
var j= 1;
$('#dataTable').find('tbody').find('tr:nth-child(j)');
it fails.
What is the problem?
Upvotes: 2
Views: 2087
Reputation: 564
You can use the .eq()
function. Indexing starts with 0, so var j = 1
will give you the second tr
var j= 1;
$('#dataTable tbody').find('tr').eq(j);
Upvotes: 2
Reputation: 15154
Theres no need for find
:-
var j= 1;
$('#dataTable tbody tr:nth-child(' + j +')');
Upvotes: 0
Reputation: 115242
Use simple string concatenation and although you can reduce it to a single selector.
var j= 1;
$('#dataTable tbody tr:nth-child(' + j + ')');
// ----^^^^^^^^^----
Upvotes: 2
Reputation: 2730
var j= 1;
$('#dataTable').find('tbody').find('tr:nth-child(' + j +')');
Upvotes: 2
Reputation: 26258
Using either the eq() function:
rows.eq(0).addClass('my_class');
Or the :eq() selector:
$('tr:eq(0)', tbl).addClass('my_class');
Upvotes: 0