7

As I know from Jquery learning, function index of jquery can be used:

  • .index() // no arguments
  • .index(domObject) // argument is a dom object
  • .index(JqueryObject) // argument is a Jquery object
  • .index("selectorString") // argument is a selector string

I'm testing index function with argument is 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);

The results are: 0 for index1 (correct) and -1 for index2 (not correct).

So, The question is: Why I can not get the correct index value of div#id2?

jsfiddle: http://jsfiddle.net/GhPxD/60/

1
  • 3
    swap it $("#id2").index('.name');, that gets the index of #id2 in the .name collection Commented Jan 6, 2015 at 10:18

3 Answers 3

3

There's no bug here, the API is just... strange. Here's the documentation :

If .index() is called on a collection of elements and a DOM element or jQuery object is passed in, .index() returns an integer indicating the position of the passed element relative to the original collection.

If a selector string is passed as an argument, .index() returns an integer indicating the position of the first element within the jQuery object relative to the elements matched by the selector. If the element is not found, .index() will return -1.

What you want is thus

$('#id2').index(".name");

Having this function behave in so different ways is very confusing IMO.

Sign up to request clarification or add additional context in comments.

2 Comments

Basically, you are correct. $('#id2').index(".name"); and $('#id1').index(".name"); return the correct result and it seems that is the way of using index function with selector string argument. But one more thing can you explain: why $('.name').index("#id1"); return the correct index? Is it accidentally correct?
The first .name element is #id1. So the index of the first .name element in the set #id1 is 0. Yes, that's accidentally correct. I'm not sure many developers can correctly use the index function with arguments at first try...
0

You can pass jquery object in index method to make this work:

var index = $('.name').index($("#id2"));

Working Demo

2 Comments

It appears the documentation is wrong as this does indeed work, yet the docs state a string selector can be provided. It could also be a bug in jQuery itself.
What you did is using the 3nd method I mention above: .index(JqueryObject) // argument is a Jquery object
-1
statement 1 :  var index1 = $('.name').index("#id1");
statement 2 :  var index2 = $('.name').index("#id2");

statement 1 1.As you know when you use any selector in jquery it return first matching element 2. now you try to find index of index("#id1") so it found sucessfully at index 0 so it return 0

statement 2 1.As you know when you use any selector in jquery it return first matching element 2. now you try to find index of index("#id2") so it unable to found any child element with #id2 so it reurn -1

1 Comment

Sorry, I dont think you are correct at this statement "when you use any selector in jquery it return first matching element". $('.name') return a Jquery object with 2 doms inside it (div#id1 and div#id2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.