Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: getting level of selected li

i'm working with wordpress+jQuery - there's a navigationscript (it's an UL) with several levels like this:

<ul>
<li>level 1 - item1</li>
<li>level 1 - item2
    <ul>
        <li>level 2 - item1</li>
        <li class=current_page_item>level 2 - item2</li>
    </ul>
</li>
<li>level 1 - item3</li>
</ul>

wordpress is applying a css-class called current_page_item to the current LI. my question: when selecting that LI - how can i find out it's level in the UL? (in my case: level 2)

like image 580
Fuxi Avatar asked Feb 22 '11 11:02

Fuxi


1 Answers

By counting its parents:

$('.current_page_item').parents('ul').length;
like image 117
David Tang Avatar answered Sep 20 '22 08:09

David Tang