Reputation: 3364
I have the html code:
<div id="all">
<div id="1">
<div id="1-1">
</div>
<div id="1-2">
</div>
</div>
<div id="2">
<div id="2-1">
</div>
</div>
</div>
I have somewhere in my JQuery code:
var obj = jQuery("#1-2");
I want to check which child of his parent (relative to his parent) is obj (in that case it should return 1 for second index).
I have tried (with no result):
alert(obj.index());
Upvotes: 2
Views: 1510
Reputation: 55750
Try this
var $obj = $("#1-2");
// Need to check th index for this structure
var $parent = $('div > div > div');
// You want to find the index of your selector
// based on the selector that follows the above structure
console.log($($parent).index($obj))
Upvotes: 0
Reputation: 87083
Your code is returning the correct index. I think you just need implement code in jquery ready.
$(function() {
var obj = jQuery("#1-2");
alert( obj.index() );
});
Upvotes: 6