MaVRoSCy
MaVRoSCy

Reputation: 17839

Target first child of parent

My html code looks like this:

...more rows
<tr class="highlightRow" id="tr0" style="display: table-row;">
    <td>0.  TIC&nbsp;</td>
    <td>123456&nbsp;</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

Answers (4)

nrsharma
nrsharma

Reputation: 2562

You can use the .closest() selector to achieve this

Try this

$('#txt0').closest('tr').find('td:first');

Upvotes: 2

Rory McCrossan
Rory McCrossan

Reputation: 337560

Instead of chaining parent() calls, use closest() with a selector:

var $firstTd = $('#txt0').closest('tr').find('td:first');

Upvotes: 2

j08691
j08691

Reputation: 207901

Use:

$('#txt0').closest('tr').find('td:first');

Upvotes: 3

GautamD31
GautamD31

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

Related Questions