Skip to content

Commit 7404513

Browse files
committed
move code into chapter and add link to jsFiddle
1 parent e64ff63 commit 7404513

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

content/misc/understanding-index.md

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,92 @@ attribution: John K. Paul
1010

1111
### <code>index()</code> with no arguments
1212

13-
<iframe style="width: 40%; height: 300px;float:left;" src="http://jsfiddle.net/johnkpaul/SrHDh/embedded/html" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
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>
1420

15-
<iframe style="width: 60%; height: 300px;float:left;" src="http://jsfiddle.net/johnkpaul/SrHDh/embedded/js" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
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)
1636

1737
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.
1838

1939
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>
2040

2141
### <code>index()</code> with a string argument
2242

23-
<iframe style="width: 40%; height: 300px;float:left;" src="http://jsfiddle.net/johnkpaul/D29cZ/embedded/html" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
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
2463

25-
<iframe style="width: 60%; height: 300px;float:left;" src="http://jsfiddle.net/johnkpaul/D29cZ/embedded/js" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
64+
var $div = $('#last');
65+
console.log('Index: ' + $div.index("div")); // 2
66+
67+
[jsFiddle demo](http://jsfiddle.net/johnkpaul/D29cZ/)
2668

2769
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.
2870

2971
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.
3072

3173
### <code>index()</code> with a jQuery object argument
3274

33-
<iframe style="width: 40%; height: 300px;float:left;" src="http://jsfiddle.net/johnkpaul/QZv7y/embedded/html" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
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/)
3498

35-
<iframe style="width: 60%; height: 300px;float:left;" src="http://jsfiddle.net/johnkpaul/QZv7y/embedded/js" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
3699

37100
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.
38101

0 commit comments

Comments
 (0)