Skip to content

Commit 13397d9

Browse files
pobockskswedberg
authored andcommitted
Added documentation for addBack, link and deprecation to andSelf
1 parent 148020b commit 13397d9

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

entries/addBack.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0"?>
2+
<entry type="method" name="addBack" return="jQuery">
3+
<title>.addBack()</title>
4+
<signature>
5+
<added>1.6</added>
6+
<argument name="selector" type="Selector" optional="true">
7+
<desc>A string containing a selector expression to match the current set of elements against.</desc>
8+
</argument>
9+
</signature>
10+
<desc>Add the previous set of elements on the stack to the current set, optionally filtered by a selector.</desc>
11+
<longdesc>
12+
<p>As described in the discussion for <code><a href="/end/">.end()</a></code>, jQuery objects maintain an internal stack that keeps track of changes to the matched set of elements. When one of the DOM traversal methods is called, the new set of elements is pushed onto the stack. If the previous set of elements is desired as well, <code>.addBack()</code> can help.</p>
13+
<p>Consider a page with a simple list on it:</p>
14+
<pre><code>
15+
&lt;ul&gt;
16+
&lt;li&gt;list item 1&lt;/li&gt;
17+
&lt;li&gt;list item 2&lt;/li&gt;
18+
&lt;li class="third-item"&gt;list item 3&lt;/li&gt;
19+
&lt;li&gt;list item 4&lt;/li&gt;
20+
&lt;li&gt;list item 5&lt;/li&gt;
21+
&lt;/ul&gt;
22+
</code></pre>
23+
<p>The result of the following code is a red background behind items 3, 4 and 5:</p>
24+
<pre><code>$('li.third-item').nextAll().addBack()
25+
.css('background-color', 'red');
26+
</code></pre>
27+
<p>First, the initial selector locates item 3, initializing the stack with the set containing just this item. The call to <code>.nextAll()</code> then pushes the set of items 4 and 5 onto the stack. Finally, the <code>.addBack()</code> invocation merges these two sets together, creating a jQuery object that points to all three items in document order: <code>{[&lt;li.third-item&gt;,&lt;li&gt;,&lt;li&gt; ]}</code>.</p>
28+
</longdesc>
29+
<example>
30+
<desc>The <code>.addBack()</code> method causes the previous set of DOM elements in the traversal stack to be added to the current set. In the first example, the top stack contains the set resulting from <code>.find("p")</code>. In the second example, <code>.addBack()</code> adds the previous set of elements on the stack — in this case <code>$("div.after-addback")</code> — to the current set, selecting both the div and its enclosed paragraphs.</desc>
31+
<code><![CDATA[
32+
$("div.left, div.right").find("div, div > p").addClass("border");
33+
34+
// First Example
35+
$("div.before-addback").find("p").addClass("background");
36+
37+
// Second Example
38+
$("div.after-addback").find("p").addBack().addClass("background");
39+
]]></code>
40+
<css><![CDATA[
41+
p, div { margin:5px; padding:5px; }
42+
.border { border: 2px solid red; }
43+
.background { background:yellow; }
44+
.left, .right { width: 45%; float: left;}
45+
.right { margin-left:3%; }
46+
]]></css>
47+
<html><![CDATA[
48+
<div class="left">
49+
<p><strong>Before <code>addBack()</code></strong></p>
50+
<div class="before-addback">
51+
<p>First Paragraph</p>
52+
<p>Second Paragraph</p>
53+
</div>
54+
</div>
55+
<div class="right">
56+
<p><strong>After <code>addBack()</code></strong></p>
57+
<div class="after-addback">
58+
<p>First Paragraph</p>
59+
<p>Second Paragraph</p>
60+
</div>
61+
</div>
62+
]]></html>
63+
</example>
64+
<category slug="traversing/miscellaneous-traversal"/>
65+
<category slug="version/1.2"/>
66+
</entry>

entries/andSelf.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?xml version="1.0"?>
2-
<entry type="method" name="andSelf" return="jQuery">
2+
<entry type="method" name="andSelf" return="jQuery" deprecated="1.8">
33
<title>.andSelf()</title>
44
<signature>
55
<added>1.2</added>
66
</signature>
77
<desc>Add the previous set of elements on the stack to the current set.</desc>
88
<longdesc>
9+
<p><strong>Note:</strong> This function has been deprecated and is now an alias for <a href="/addBack/"><code>.addBack()</code></a>, which should be used with jQuery 1.8 and later.</p>
910
<p>As described in the discussion for <code><a href="http://api.jquery.com/end/">.end()</a></code>, jQuery objects maintain an internal stack that keeps track of changes to the matched set of elements. When one of the DOM traversal methods is called, the new set of elements is pushed onto the stack. If the previous set of elements is desired as well, <code>.andSelf()</code> can help.</p>
1011
<p>Consider a page with a simple list on it:</p>
1112
<pre><code>

0 commit comments

Comments
 (0)