views:

71

answers:

2

hi all,

i'm having this markup:

<ul class=container>

    <li rec=1>
        <div>
           <div>
               <div>i'm here</div>
           </div>
        </div>
    </li>

</ul>

now i'm at the div element ("i'm here"), how can i get reference to the li? i tried div.parent("li"); but didntwork ..

+3  A: 

Use

closest("li")

rather than parent http://docs.jquery.com/Traversing/closest Parent only gets the direct parent.

stimms
awesome, works like a charm - thx!
Fuxi
If the hierarchy levels are known, but you don't know what type of tag the great-grandparent (the li in this case) will be, then you can do this: $("div").contains("i'm here").parent().parent().parent();
micahwittman
+1  A: 

parent is used to get the unique direct antecesor of an element, which in your case is a div.

You should use parents or closest:

div.parents("li");
CMS