Skip to content

Commit 87babec

Browse files
committed
prevAll: Document issues with reverse doc order
`.prevAll()` returns elements in the reverse document order. This can pose issues when used with APIs like `.append()` or `.wrapAll()`. Document how to deal with the issue with help from `jQuery.uniqueSort()`. Ref jquery/jquery#5149
1 parent 57a7ff7 commit 87babec

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

entries/prevAll.xml

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<desc>A string containing a selector expression to match elements against.</desc>
88
</argument>
99
</signature>
10-
<desc>Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.</desc>
10+
<desc>Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector, in the reverse document order.</desc>
1111
<longdesc>
1212
<p>Given a jQuery object that represents a set of DOM elements, the <code>.prevAll()</code> method searches through the predecessors of these elements in the DOM tree and construct a new jQuery object from the matching elements; the elements are returned in order beginning with the closest sibling.</p>
1313
<p>The method optionally accepts a selector expression of the same type that we can pass to the <code>$()</code> function. If the selector is supplied, the elements will be filtered by testing whether they match it.</p>
@@ -26,6 +26,36 @@
2626
$( "li.third-item" ).prevAll().css( "background-color", "red" );
2727
</code></pre>
2828
<p>The result of this call is a red background behind items 1 and 2. Since we do not supply a selector expression, these preceding elements are unequivocally included as part of the object. If we had supplied one, the elements would be tested for a match before they were included.</p>
29+
<p><strong>Note:</strong> Many APIs, like <code><a href="/append/">append</a></code> or <code><a href="/wrapAll/">wrapAll</a></code> process node in the order in which they appear in the jQuery object. This can pose issues with APIs like <code>.prevAll()</code> in which the reverse document order is used. Consider the following example:</p>
30+
<pre><code>
31+
&lt;div&gt;
32+
&lt;div&gt;First&lt;/div&gt;
33+
&lt;div&gt;Second&lt;/div&gt;
34+
&lt;div class="last-item"&gt;Last&lt;/div&gt;
35+
&lt;/div&gt;
36+
</code></pre>
37+
<p>The following call:</p>
38+
<pre><code>
39+
$( ".last-item" )
40+
.prevAll()
41+
.wrapAll( "&lt;div class='wrapper'&gt;&lt;/div&gt;" );
42+
</code></pre>
43+
<p>would result in the following HTML:</p>
44+
<pre><code>
45+
&lt;div&gt;
46+
&lt;div class="wrapper"&gt;
47+
&lt;div&gt;Second&lt;/div&gt;
48+
&lt;div&gt;First&lt;/div&gt;
49+
&lt;/div&gt;
50+
&lt;div class="last-item"&gt;Last&lt;/div&gt;
51+
&lt;/div&gt;
52+
</code></pre>
53+
<p>because "Item 2" gets appended to the wrapper div first. To work around the issue, you may use <code><a href="/jQuery.uniqueSort/">$.uniqueSort()</a></code> on the <code>.prevAll()</code> output first:</p>
54+
<pre><code>
55+
var prevSiblings = $( ".last-item" ).prevAll();
56+
$.uniqueSort( prevSiblings );
57+
prevSiblings.wrapAll( "&lt;div class='wrapper'&gt;&lt;/div&gt;" );
58+
</code></pre>
2959
</longdesc>
3060
<example>
3161
<desc>Locate all the divs preceding the last div and give them a class.</desc>
@@ -50,6 +80,69 @@ $( "div" ).last().prevAll().addClass( "before" );
5080
<div></div>
5181
<div></div>
5282
<div></div>
83+
]]></html>
84+
</example>
85+
<example>
86+
<desc>Locate all the divs preceding the last item and wrap them with a div with class <code>wrapper</code> - with or without <code><a href="/jQuery.uniqueSort/">$.uniqueSort()</a></code>.</desc>
87+
<code><![CDATA[
88+
$( "#container-1" )
89+
.find( ".item" )
90+
.last()
91+
.prevAll()
92+
.wrapAll( "<div class='wrapper' data-content='No uniqueSort'></div>" );
93+
94+
var prevSiblings = $( "#container-2" )
95+
.find( ".item" )
96+
.last()
97+
.prevAll();
98+
$.uniqueSort( prevSiblings );
99+
prevSiblings.wrapAll( "<div class='wrapper' data-content='With uniqueSort'></div>" );
100+
]]></code>
101+
<css><![CDATA[
102+
body {
103+
display: flex;
104+
}
105+
.container {
106+
display: flex;
107+
margin: 10px 50px 10px 10px;
108+
}
109+
.wrapper {
110+
position: relative;
111+
display: flex;
112+
padding: 30px 10px 10px 10px;
113+
background: #def;
114+
border: 2px solid black;
115+
}
116+
.wrapper::before {
117+
content: attr(data-content);
118+
position: absolute;
119+
top: 15px;
120+
left: 15px;
121+
}
122+
.item {
123+
display: flex;
124+
justify-content: center;
125+
align-items: center;
126+
width: 70px;
127+
height: 70px;
128+
background: #abc;
129+
border: 2px solid black;
130+
margin: 10px;
131+
font-size: 50px;
132+
}
133+
]]></css>
134+
<html><![CDATA[
135+
<div class="container" id="container-1">
136+
<div class="item">1</div>
137+
<div class="item">2</div>
138+
<div class="item">3</div>
139+
</div>
140+
141+
<div class="container" id="container-2">
142+
<div class="item">1</div>
143+
<div class="item">2</div>
144+
<div class="item">3</div>
145+
</div>
53146
]]></html>
54147
</example>
55148
<category slug="traversing/tree-traversal"/>

0 commit comments

Comments
 (0)