Jump to content

jQueryObj.index() doesn't follow the documentation?


graduate

Recommended Posts

In Documentation I read: "If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements." But the .index() method returns the position of the LAST element within the jQuery object relative to its sibling elements. I've checked it in Firefox and Chrome.

  1. <div>
  2. <p>***</p>
  3. <p>***</p>
  4. <p>***</p>
  5. </div>
  6. $('div p').index(); // 2

Link to comment
Share on other sites

You are not targeting a specific element with class or id, but all! so it will show the index value for the last found. look at below example where the target specific element based on class, id or if clicked it will return that specific index

<div><p>***</p><p class="thisone">***</p><p>***</p></div><span></span><script>//var barIndex = $('#bar li').index();var barIndex = $('div p.thisone').index();$('span').html( 'Index: ' +  barIndex );$("div p").click(function(){alert($(this).index());});</script>

Link to comment
Share on other sites

You can't use class in that situation as you would be searching through multiple classes, you need something unique about the element in question to target, when you want to target all p elements, or p elements with class name, you would use $("p") or $("p.classname"), that is what it will do, seek out all p or p with class and apply whatever, you are go through all p and retrieving index ref, that's exactly what it has done and the last index is 2. By your thinking it should always a return definitive 0, so why did they not state exactly that!

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.
their example explaining this
If we omit the argument, .index() will return the position of the first element within the set of matched elements in relation to its siblings: <ul>  <li id="foo">foo</li>  <li id="bar">bar</li>  <li id="baz">baz</li></ul> alert('Index: ' + $('#bar').index(); Again, we get back the zero-based position of the list item: Index: 1

Link to comment
Share on other sites

In all your examples you invoke .index() on an jQuery object with just one element. Invoke it on an object with several elements. Documentation says that it should return the sibling position of the first element in the list, but it does it for the last element.For instance:jq = $('p');jq.length; // 5jq.index(); // should be the same as $( jq[0] ).index();// but it works as $( jq[4] ).index();

Link to comment
Share on other sites

you are invoking it to target a specific sibling elements $( jq[0] ).index(); will be the first (0), as will $( jq[4] ).index(); will index the 4 element as index (4), $('p'); on its own has no such specific target so it will list through all, the last one will be index 4 value returned. Think about it, when you want the index ref of a specific element using index(), you don't just use the tag name, YOU HAVE to use a way of singling out a specific element by id, singular class ref, or onclick event otherwise it is pointless function, that exactly what all the example provide at jquery website for index().

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...