Suppose you don't have a selector (id, class, etc.), you need a simple loop in elements that don't have ul.
var maxDepth = -1
, cDepth = -1
;
$("li:not(:has(ul))").each(function() {
cDepth = $(this).parents("ul").length;
if (cDepth > maxDepth) {
maxDepth = cDepth;
}
});
alert(maxDepth);
JSFIDDLE
If you have a .myClassselector for the deepest li:
<ul>
<li></li>
<li>
<ul>
<li></li>
</ul>
</li>
<li>
<ul>
<li>
<ul>
<li class="myClass"></li>
</ul>
</li>
</ul>
</li>
</ul>
it's very simple: just count his parents ul
var depth = $(".myClass").parents("ul").length;
source
share