Skip to content

Commit 20172f6

Browse files
committed
.each(): Explain implicit iteration. Fixes jquery#47
1 parent 5edb855 commit 20172f6

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

entries/each.xml

+31-22
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,40 @@
1010
<desc>Iterate over a jQuery object, executing a function for each matched element. </desc>
1111
<longdesc>
1212
<p>The <code>.each()</code> method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword <code>this</code> refers to the element.</p>
13-
<p>Suppose we had a simple unordered list on the page:</p>
13+
<p>Suppose you have a simple unordered list on the page:</p>
1414
<pre><code>&lt;ul&gt;
1515
&lt;li&gt;foo&lt;/li&gt;
1616
&lt;li&gt;bar&lt;/li&gt;
1717
&lt;/ul&gt;
1818
</code></pre>
19-
<p>We can select the list items and iterate across them:</p>
20-
<pre><code>$('li').each(function(index) {
21-
alert(index + ': ' + $(this).text());
19+
<p>You can select the list items and iterate across them:</p>
20+
<pre><code>$( "li" ).each(function( index ) {
21+
console.log( index + ": "" + $(this).text() );
2222
});
2323
</code></pre>
24-
<p>A message is thus alerted for each item in the list:</p>
24+
<p>A message is thus logged for each item in the list:</p>
2525
<p>
2626
<span class="output">0: foo</span>
2727
<br/>
2828
<span class="output">1: bar</span>
2929
</p>
30-
<p>We can stop the loop from within the callback function by returning <code>false</code>.</p>
30+
<p>You can stop the loop from within the callback function by returning <code>false</code>.</p>
31+
<p>Note: most jQuery methods that return a jQuery object also loop through the set of elements in the jQuery collection — a process known as <i>implicit iteration</i>. When this occurs, it is often unnecessary to <i>explicitly</i> iterate with the <code>.each()</code> method:</p>
32+
<pre><code>// The .each() method is unnecessary here:
33+
$( "li" ).each(function() {
34+
$(this).addClass( "foo" );
35+
});
36+
37+
// Instead, you should rely on implicit iteration:
38+
$( "li" ).addClass( "bar" );
39+
</code></pre>
3140
</longdesc>
3241
<example>
33-
<desc>Iterates over three divs and sets their color property.</desc>
42+
<desc>Iterate over three divs and sets their color property.</desc>
3443
<code><![CDATA[
3544
$(document.body).click(function () {
36-
$("div").each(function (i) {
37-
if (this.style.color != "blue") {
45+
$( "div" ).each(function (i) {
46+
if ( this.style.color != "blue" ) {
3847
this.style.color = "blue";
3948
} else {
4049
this.style.color = "";
@@ -43,19 +52,19 @@
4352
});
4453
]]></code>
4554
<css><![CDATA[
46-
div { color:red; text-align:center; cursor:pointer;
55+
div { color:red; text-align:center; cursor:pointer;
4756
font-weight:bolder; width:300px; }
4857
]]></css>
4958
<html><![CDATA[<div>Click here</div>
5059
<div>to iterate through</div>
5160
<div>these divs.</div>]]></html>
5261
</example>
5362
<example>
54-
<desc>If you want to have the jQuery object instead of the regular DOM element, use the $(this) function, for example:</desc>
63+
<desc>To access a jQuery object instead of the regular DOM element, use <code>$(this)</code>. For example:</desc>
5564
<code><![CDATA[
56-
$("span").click(function () {
57-
$("li").each(function(){
58-
$(this).toggleClass("example");
65+
$( "span" ).click(function () {
66+
$( "li" ).each(function(){
67+
$( this ).toggleClass( "example" );
5968
});
6069
});
6170
@@ -74,14 +83,14 @@
7483
</ul>]]></html>
7584
</example>
7685
<example>
77-
<desc>You can use 'return' to break out of each() loops early.</desc>
86+
<desc>Use "return" to break out of each() loops early.</desc>
7887
<code><![CDATA[
79-
$("button").click(function () {
80-
$("div").each(function (index, domEle) {
88+
$( "button" ).click(function () {
89+
$( "div" ).each(function ( index, domEle) {
8190
// domEle == this
82-
$(domEle).css("backgroundColor", "yellow");
83-
if ($(this).is("#stop")) {
84-
$("span").text("Stopped at div index #" + index);
91+
$( domEle ).css( "backgroundColor", "yellow" );
92+
if ( $(this).is( "#stop" ) ) {
93+
$( "span" ).text( "Stopped at div index #" + index );
8594
return false;
8695
}
8796
});
@@ -93,7 +102,7 @@
93102
border:2px blue solid; text-align:center; }
94103
span { color:red; }
95104
]]></css>
96-
<html><![CDATA[<button>Change colors</button>
105+
<html><![CDATA[<button>Change colors</button>
97106
<span></span>
98107
<div></div>
99108
<div></div>
@@ -109,4 +118,4 @@
109118
<category slug="miscellaneous/collection-manipulation"/>
110119
<category slug="traversing"/>
111120
<category slug="version/1.0"/>
112-
</entry>
121+
</entry>

0 commit comments

Comments
 (0)