Reputation: 5158
<ul>
<li><div id="some1">.....</div></li>
<li><div id="some22">.....</div></li>
<li><div id="somed">.....</div></li>
<li><div id="some54">.....</div></li>
<li><div id="some77">.....</div></li>
<li><div id="some32">.....</div></li>
<li><div id="some2">.....</div></li>
</ul>
In the above structure, I want to find the number or index+1 of the li
whose child is #somed. so basically for #somed
I want to get a value of 3. Any ideas how to do this in jquery?
Upvotes: 0
Views: 84
Reputation: 67802
Try
$('#someId').parent().index()+1
Since your elements are wrapped, you'll need to test off of the list elements containing your divs. You might consider putting the ids on the li
s and figuring out if you really need divs at all, but that's another discussion.
Upvotes: 0
Reputation: 253308
This answer posted as a more generalised solution, that should work for all li
elements with a child div
with an id
starting with 'some':
$('div[id^="some"]').click(
function(){
var indexOfParent = $(this).closest('li').index();
alert(indexOfParent); // alerts '2', due to zero-based JavaScript arrays.
});
If you need it to be 3
, rather than the native JavaScript index (zero-based), then you'll have to explicitly add 1
to the variable.
Upvotes: 0