JQuery: getting the level of the selected li

I work with wordpress + jQuery - there is navigationscript (this is UL) with several levels, such as:

<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 applies a css class called current_page_item to the current LI. my question is: when choosing this LI - how can I find out its level in UL? (in my case: level 2)

thank

+3
source share
2 answers

Considering her parents:

$('.current_page_item').parents('ul').length;
+14
source
var selectedItem = $('.current_page_item');
var Index = $("li", "ul").index(selectedItem);

The pointer gives you the index of this class.

+1
source

Source: https://habr.com/ru/post/1793844/


All Articles