Reputation: 17839
My html code looks like this:
...more rows
<tr class="highlightRow" id="tr0" style="display: table-row;">
<td>0. TIC </td>
<td>123456 </td>
<td>
<input type="text" value="12345" id="txt0" name="txtTicRequired0" class="tic">
</td>
<td>
<input type="checkbox" onclick="checkIfChecked(0);" checked="" id="ck0">
</td>
</tr>
...more rows
My starting point is the id of the input field at the 3rd td
element and I want to target the first td
of the tr
.
To go to the tr
I use: $('#txt0').parent().parent();
But how can I go from that parent to the first td
element using the first-clild selector?
Upvotes: 1
Views: 118
Reputation: 2562
You can use the .closest() selector to achieve this
Try this
$('#txt0').closest('tr').find('td:first');
Upvotes: 2
Reputation: 337560
Instead of chaining parent()
calls, use closest()
with a selector:
var $firstTd = $('#txt0').closest('tr').find('td:first');
Upvotes: 2
Reputation: 28763
Try like
$('#txt0').parent().parent().find('td:eq(0)');
Or try with first
instead of eq
like
$('#txt0').parent().parent().find('td:first');
Upvotes: 1