Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: selector question (parent)

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 ..

like image 270
Fuxi Avatar asked Dec 06 '25 18:12

Fuxi


2 Answers

Use

closest("li") 

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

like image 130
stimms Avatar answered Dec 08 '25 11:12

stimms


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");
like image 42
Christian C. Salvadó Avatar answered Dec 08 '25 13:12

Christian C. Salvadó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!