|
| 1 | +--- |
| 2 | +title : using jQuery's index() function |
| 3 | +github: johnkpaul |
| 4 | +level: intermediate |
| 5 | +attribution: John K. Paul |
| 6 | +--- |
| 7 | +## .index() |
| 8 | + |
| 9 | +<code>index()</code> is a method on jQuery objects that is generally used to search for a given element within the jQuery object that it is called on. This method has four different signatures with different semantics that can potentially cause confusion. This chapter will describe the details for how to understand how <code>.index()</code> works with each signature. |
| 10 | + |
| 11 | +### <code>index()</code> with no arguments |
| 12 | + |
| 13 | + <ul> |
| 14 | + <div></div> |
| 15 | + <li id="foo1">foo</li> |
| 16 | + <li id="bar1">bar</li> |
| 17 | + <li id="baz1">baz</li> |
| 18 | + <div></div> |
| 19 | + </ul> |
| 20 | + |
| 21 | + var $foo = $('#foo1'); |
| 22 | + console.log('Index: ' + $foo.index()); // 1 |
| 23 | + |
| 24 | + var $listItem = $('li'); |
| 25 | + // this implicitly calls .last() |
| 26 | + console.log('Index: ' + $listItem.index()); // 3 |
| 27 | + console.log('Index: ' + $listItem.last().index()); // 3 |
| 28 | + |
| 29 | + var $div = $('div'); |
| 30 | + |
| 31 | + // this implicitly calls .last() |
| 32 | + console.log('Index: ' + $div.index()); // 4 |
| 33 | + console.log('Index: ' + $div.last().index()); // 4 |
| 34 | + |
| 35 | +[jsFiddle demo](http://jsfiddle.net/johnkpaul/SrHDh) |
| 36 | + |
| 37 | +In the first example, <code>index()</code> gives the zero-based index of <code>#foo1</code> within it's parent. since <code>#foo1</code> is the second child of it's parent, <code>index()</code> returns 1. |
| 38 | + |
| 39 | +The first potential confusion comes from the other examples in this fiddle. When <code>index()</code> is called on a jquery object that contains more than one element, it does not calculate the index of the first element, as I would have expected, but rather calculates the index of the last element. This is equivalent to always calling <code>$jqObject.last().index();</code> |
| 40 | + |
| 41 | +### <code>index()</code> with a string argument |
| 42 | + |
| 43 | + <ul> |
| 44 | + <div class="test"></div> |
| 45 | + <li id="foo1">foo</li> |
| 46 | + <li id="bar1" class="test">bar</li> |
| 47 | + <li id="baz1">baz</li> |
| 48 | + <div class="test"></div> |
| 49 | + </ul> |
| 50 | + <div id="last"></div> |
| 51 | + |
| 52 | + var $foo = $('li'); |
| 53 | + |
| 54 | + // this implicitly calls .first() |
| 55 | + console.log('Index: ' + $foo.index("li")); // 0 |
| 56 | + console.log('Index: ' + $foo.first().index("li")); // 0 |
| 57 | + |
| 58 | + var $baz = $('#baz1'); |
| 59 | + console.log('Index: ' + $baz.index("li")); // 2 |
| 60 | + |
| 61 | + var $listItem = $('#bar1'); |
| 62 | + console.log('Index: ' + $listItem.index(".test")); // 1 |
| 63 | + |
| 64 | + var $div = $('#last'); |
| 65 | + console.log('Index: ' + $div.index("div")); // 2 |
| 66 | + |
| 67 | +[jsFiddle demo](http://jsfiddle.net/johnkpaul/D29cZ/) |
| 68 | + |
| 69 | +When <code>index()</code> is called with a string argument, there are two things to consider. The first is that jQuery will implicitly call <code>.first()</code> on the original jQuery object. It will be finding the index of the first element, not the last element in this case. This inconsistency always makes me stop and think, so be careful with this one. |
| 70 | + |
| 71 | +The second point is that jQuery is querying the entire dom using the passed in string selector and checking the index within that newly queried jQuery object. For example, when using <code>.index("div")</code> in the last example, jQuery is selecting all of the divs in the document, and then searching for the index that contains the first element in the jquery object that <code>.index()</code> is called on. |
| 72 | + |
| 73 | +### <code>index()</code> with a jQuery object argument |
| 74 | + |
| 75 | + <ul> |
| 76 | + <div class="test"></div> |
| 77 | + <li id="foo1">foo</li> |
| 78 | + <li id="bar1" class="test">bar</li> |
| 79 | + <li id="baz1">baz</li> |
| 80 | + <div class="test"></div> |
| 81 | + </ul> |
| 82 | + <div id="last"></div> |
| 83 | + |
| 84 | + var $foo = $('li'); |
| 85 | + var $baz = $('#baz1'); |
| 86 | + |
| 87 | + console.log('Index: ' + $foo.index($baz)); // 2 |
| 88 | + |
| 89 | + var $tests = $(".test"); |
| 90 | + var $bar = $("#bar1"); |
| 91 | + |
| 92 | + // implicitly calls .first() on the argument |
| 93 | + console.log('Index: ' + $tests.index($bar)); // 1 |
| 94 | + |
| 95 | + console.log('Index: ' + $tests.index($bar.first())); // 1 |
| 96 | + |
| 97 | +[jsFiddle demo](http://jsfiddle.net/johnkpaul/QZv7y/) |
| 98 | + |
| 99 | + |
| 100 | +In this case, the first element of the jQuery object that is passed into <code>.index()</code> is being checked against all of the elements in the original jQuery object. The original jQuery object, on the left side of <code>.index()</code>, is array-like and is searched from index 0 through <code>length - 1</code> for the first element of the argument jQuery object. |
| 101 | + |
| 102 | +### <code>index()</code> with a DOM element argument |
| 103 | + |
| 104 | +In this case, the DOM element that is passed into <code>.index()</code> is being checked against all of the elements in the original jQuery object. Once all of the other cases are understood, this should be the simplest case. It is very similar to the previous case, except since the DOM element is passed directly, it is not taken from a jQuery object container. |
0 commit comments