2

What am I missing regarding JQuery's index() method. Based on the documentation, I would expect to be alerted a value of 1 here:

HTML

<div class="test" id="div1"></div>
<div class="test" id="div2"></div>

JS

$(document).ready(function(){
    var index = $('.test').index('#div2');
    alert(index);
});

Here's a fiddle: http://jsfiddle.net/cpody1cb/

Why am I alerted -1? If I instead use index('#div1'), I am alerted 0, as I would expect.

2
  • you're selectors are backwards. $('#div2').index('.test') Commented Nov 20, 2014 at 22:02
  • The documentation says 'If we use a string as the .index() method's argument, it is interpreted as a jQuery selector string. The first element among the object's matched elements which also matches this selector is located.' I still don't understand why what I'm doing doesn't work. Commented Nov 20, 2014 at 22:03

3 Answers 3

2

You're trying too hard.

var index = $('#div2').index();

To explain why yours doesn't work, you simply have the selectors backward. The argument for the index() method should be a selector that gets the set of elements, not the element for which you want the index. This would be done like so:

var index = $('#div2').index('.test');
Sign up to request clarification or add additional context in comments.

6 Comments

Are you sure that he wants index of element in page and not index in selection?
I want the index in a selection, not on page.
Based on the documentation I still don't understand why what I'm doing doesn't work. It says "If we use a string as the .index() method's argument, it is interpreted as a jQuery selector string. The first element among the object's matched elements which also matches this selector is located."
the jquery docs actually says the opposite , I see why OP is confused , look from docs : var index = $("div").index(this); this meaning the element clicked. "div" is the collection , and this is the specific one to get the index of
@flyingL123 - personally I have never needed .index() ever , and I have made many many many sites with complex jquery . So personally I would recommend to not use it , because this looks like a bug in the docs , or in the code and it may be changed some day. There are so many other ways to accomplish whatever you need the index for
|
0

Since the indexes are zero-based you should get 1

$(document).ready(function(){
    var index = $('.test').index( $('#div2') );
    alert(index);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="test" id="div1"></div>
<div class="test" id="div2"></div>

Comments

0

.index() can only work on one element, so it starts by reducing the set of selected elements to only the first one, which explains why you get 0 when you use #div1 and -1 when you use #div2. You should be selecting the div you want the index of, and then giving it a selector to tell it where to look for the element.

$('#div2').index('.test')

1 Comment

That is not the way it is explained in the doc's , I see why OP is confused they say var index = $("li").index(listItemClicked) , so "li" is the collection , and itemClicked is the element you want the index of , your example works , but it opposite the way the offical docs say to do it

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.