JQuery Parent Selectors

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

For instance:

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

to find the line from the div I'm using:

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

It seems to me that I should write something like

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

but I can not find such a function in the docs.

+5
source share
3 answers

You can use .closest()for this:

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

If this helps, there is a certain category to narrow down your future searches: Tree Traverse

+6
source
$(element).parents('TR:first');

Edit: Or what Nick said below - forgot about this suction cup.

+1
source
$('#foo').parent('tr')
0

All Articles