views:

51

answers:

2

hi all,

here's my markup:

<div class=container>
    <ul>
        <li class=record>
            <div class=item></div>    <---- I am here
        </li>
    </ul>
</div>

the container is always present, but the UL is optional. what i want to do is get the "next parent" - element (from my current item), either li.record (if present) or div .container

the problem is to keep the hierarchy div.container > li.record

how can i check for that? (select UL, if not present select container)

thx

+3  A: 

This should work:

$.closest("ul, .container");
Jonathan Sampson
great - thanks!!
Fuxi
+4  A: 

You can use either the generic parent method:

$("li.record").parent("ul, div.container");

That will get you the direct parent or nothing if the parent doesn't match either selector.

OR

the closest method:

$("li.record").closest("ul, div.container");

Which will get you the closest ancestor even if it's not the direct parent..

Gabriel Hurley