Determine what number li element a div is in

Say I have:

<ul>
  <li></li>
  <li>
    <div id="test"></div>
  </li>
  <li></li>
</ul>

Is there a way to tell from the div tag what number li tag it’s in?

I.e. in the div there is a javascript function that returns 2 since it’s inside the second li element.

I realise I could go:

  • get parent ul
  • check each li child until the div is found.

Is there a more elegant way to do this?


Source: jquery

Leave a Reply