/
filter.xml
149 lines (149 loc) · 5.84 KB
/
filter.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?xml version="1.0"?>
<entry type="method" name="filter" return="jQuery">
<title>.filter()</title>
<signature>
<added>1.0</added>
<argument name="selector" type="Selector">
<desc>A string containing a selector expression to match the current set of elements against.</desc>
</argument>
</signature>
<signature>
<added>1.0</added>
<argument name="function" type="Function">
<desc>A function used as a test for each element in the set. <code>this</code> is the current DOM element.</desc>
<argument name="index" type="Integer" />
<argument name="element" type="Element" />
<return type="Boolean" />
</argument>
</signature>
<signature>
<added>1.4</added>
<argument name="elements" type="Element">
<desc>One or more DOM elements to match the current set of elements against.</desc>
</argument>
</signature>
<signature>
<added>1.4</added>
<argument name="selection" type="jQuery">
<desc>An existing jQuery object to match the current set of elements against.</desc>
</argument>
</signature>
<desc>Reduce the set of matched elements to those that match the selector or pass the function's test. </desc>
<longdesc><p>Given a jQuery object that represents a set of DOM elements, the <code>.filter()</code> method constructs a new jQuery object from a subset of the matching elements. The supplied selector is tested against each element; all elements matching the selector will be included in the result.</p><p>Consider a page with a simple list on it:</p>
<pre><code>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
<li>list item 6</li>
</ul>
</code></pre>
<p>We can apply this method to the set of list items:</p>
<pre><code>
$( "li" ).filter( ":even" ).css( "background-color", "red" );
</code></pre>
<p>The result of this call is a red background for items 1, 3, and 5, as they match the selector (recall that <code>:even</code> and <code>:odd</code> use 0-based indexing).</p>
<h4 id="using-filter-function">Using a Filter Function</h4>
<p>The second form of this method allows us to filter elements against a function rather than a selector. For each element, if the function returns <code>true</code> (or a "truthy" value), the element will be included in the filtered set; otherwise, it will be excluded. Suppose we have a somewhat more involved HTML snippet:</p>
<pre><code>
<ul>
<li><strong>list</strong> item 1 - one strong tag</li>
<li><strong>list</strong> item <strong>2</strong> -
two <span>strong tags</span></li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
<li>list item 6</li>
</ul>
</code></pre>
<p>We can select the list items, then filter them based on their contents:</p>
<pre><code>
$( "li" )
.filter(function( index ) {
return $( "strong", this ).length === 1;
})
.css( "background-color", "red" );
</code></pre>
<p>This code will alter the first list item only, as it contains exactly one <code><strong></code> tag. Within the filter function, <code>this</code> refers to each DOM element in turn. The parameter passed to the function tells us the index of that DOM element within the set matched by the jQuery object.</p>
<p>We can also take advantage of the <code>index</code> passed through the function, which indicates the 0-based position of the element within the unfiltered set of matched elements:</p>
<pre><code>
$( "li" )
.filter(function( index ) {
return index % 3 === 2;
})
.css( "background-color", "red" );
</code></pre>
<p>This alteration to the code will cause the third and sixth list items to be highlighted, as it uses the modulus operator (<code>%</code>) to select every item with an <code>index</code> value that, when divided by 3, has a remainder of <code>2</code>.</p>
</longdesc>
<example>
<desc>Change the color of all divs; then add a border to those with a "middle" class.</desc>
<code><![CDATA[
$( "div" )
.css( "background", "#c8ebcc" )
.filter( ".middle" )
.css( "border-color", "red" );
]]></code>
<css><![CDATA[
div {
width: 60px;
height: 60px;
margin: 5px;
float: left;
border: 2px white solid;
}
]]></css>
<html><![CDATA[
<div></div>
<div class="middle"></div>
<div class="middle"></div>
<div class="middle"></div>
<div class="middle"></div>
<div></div>
]]></html>
</example>
<example>
<desc>Change the color of all divs; then add a border to the second one (index == 1) and the div with an id of "fourth."</desc>
<code><![CDATA[
$( "div" )
.css( "background", "#b4b0da" )
.filter(function( index ) {
return index === 1 || $( this ).attr( "id" ) === "fourth";
})
.css( "border", "3px double red" );
]]></code>
<css><![CDATA[
div {
width: 60px;
height: 60px;
margin: 5px;
float: left;
border: 3px white solid;
}
]]></css>
<html><![CDATA[
<div id="first"></div>
<div id="second"></div>
<div id="third"></div>
<div id="fourth"></div>
<div id="fifth"></div>
<div id="sixth"></div>
]]></html>
</example>
<example>
<desc>Select all divs and filter the selection with a DOM element, keeping only the one with an id of "unique".</desc>
<code><![CDATA[
$( "div" ).filter( document.getElementById( "unique" ) );
]]></code>
</example>
<example>
<desc>Select all divs and filter the selection with a jQuery object, keeping only the one with an id of "unique".</desc>
<code><![CDATA[
$( "div" ).filter( $( "#unique" ) );
]]></code>
</example>
<category slug="traversing/filtering"/>
<category slug="version/1.0"/>
<category slug="version/1.4"/>
</entry>