As I know from jQuery training, you can use the jquery function index:
.index() // without arguments.index(domObject) // argument is a dom object.index(JqueryObject) // An argument is a jQuery object.index("selectorString") // the argument is a selector string
I am testing an index function with an argument - a selector string.
html → body:
<div class="name" id="id1"> <h2>Z</h2> <h2>X</h2> </div> <div class="name" id="id2"> <h2>A</h2> <h2>B</h2> </div>
Jquery scripts:
var index1 = $('.name').index("#id1"); var index2 = $('.name').index("#id2"); alert(index1); alert(index2);
Results: 0 for index1 (correct) and -1 for index2 (incorrect).
So the question is: why can't I get the correct div # id2 index value?
jsfiddle: http://jsfiddle.net/GhPxD/60/
source share