Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Index function with selector string

Tags:

jquery

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/

like image 726
Chris Phan Avatar asked Jan 06 '15 10:01

Chris Phan


People also ask

How to get index value in jQuery?

jQuery Misc index() Method The index() method returns the index position of specified elements relative to other specified elements. The elements can be specified by jQuery selectors, or a DOM element. Note: If the element is not found, index() will return -1.

How to give index in jQuery?

The index() is an inbuilt method in jQuery which is used to return the index of the a specified elements with respect to selector. Parameter: It accepts an optional parameter “element” which is used to get the position of the element. Return value: It returns an integer denoting the index of the specified element.

How do you select an element by index?

The :eq() selector selects an element with a specific index number. The index numbers start at 0, so the first element will have the index number 0 (not 1). This is mostly used together with another selector to select a specifically indexed element in a group (like in the example above).

Which of the following CSS selectors will select the second div in jQuery?

myclass:eq(1)" ) selects the second element in the document with the class myclass, rather than the first. In contrast, :nth-child(n) uses 1-based indexing to conform to the CSS specification. Prior to jQuery 1.8, the :eq(index) selector did not accept a negative value for index (though the . eq(index) method did).


1 Answers

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.

like image 69
Denys Séguret Avatar answered Nov 15 '22 00:11

Denys Séguret