fearofawhackplanet
fearofawhackplanet

Reputation: 53396

jQuery parent selectors

Is there a jQuery parent selector that traverses up the DOM until the first match is found?

Eg:

<tr>
    <td>
        <div id="foo">hello!</div>
    </td>
</tr>

to find the row from the div I am using:

$('#foo').parent().parent();

It feels like I should be able to write something like

$('#foo').firstParent('tr');

but I can't find any such function in the docs.

Upvotes: 5

Views: 1588

Answers (3)

Nick Craver
Nick Craver

Reputation: 630429

You can use .closest() for this:

$('#foo').closest('tr');

If it helps, there's a category specifically for this to narrow your future searches to: Tree Traversal

Upvotes: 6

Jason
Jason

Reputation: 3030

$(element).parents('TR:first');

Edit: Or what Nick said below - forgot about that sucker.

Upvotes: 1

dockeryZ
dockeryZ

Reputation: 3981

$('#foo').parent('tr')

Upvotes: 0

Related Questions