Ilja
Ilja

Reputation: 46479

Finding what child the nested ul is based on link clicked inside it

I have a setup like this

<ul>
  <li> <a href="#">link</a> 
       <ul>
         <li> <a href="#">link</a>
         <ul> ... etc
       </ul>
  </li>
</ul>

So several nested uls, I need to figure out some way to find what level of nesting the ul has based on link that was clicked inside it, is it a top (1st) middle (2nd) etc.. one

Upvotes: 2

Views: 58

Answers (2)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

You can use

$("a").click(function(){
  alert($(this).parents("ul").length);
});

Fiddle

Upvotes: 3

suvroc
suvroc

Reputation: 3062

If you have clicked element <li> (in your event handler) you can just count all parent <ul> elements

element.parents('ul').length

Upvotes: 4

Related Questions