What's the easiest way to get the depth of an element in pure JavaScript or jQuery? By "depth" I mean how many elements deep is it nested, or how many ancestors does it have.
How about:
$('#my-element').parents().length
An additional note. If you want to get the depth relative to a certain context you can do:
var depth = $("#my-element","#ContextContainerID").parents("ul").length;
Above, I'm searching for how many UL's are within the container #ContextContainerID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With