<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' href='docs-1.7.xsl'?>
<entries><entry type='method' name="filter" return="jQuery">
                <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(index)" 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>
                </signature>
                <signature>
                  <added>1.4</added>
                  <argument name="element" type="Element">
                    <desc>An element to match the current set of elements against.</desc>
                  </argument>
                </signature>
                <signature>
                  <added>1.4</added>
                  <argument name="jQuery object" type="Object">
                    <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>
&lt;ul&gt;
  &lt;li&gt;list item 1&lt;/li&gt;
  &lt;li&gt;list item 2&lt;/li&gt;
  &lt;li&gt;list item 3&lt;/li&gt;
  &lt;li&gt;list item 4&lt;/li&gt;
  &lt;li&gt;list item 5&lt;/li&gt;
  &lt;li&gt;list item 6&lt;/li&gt;
&lt;/ul&gt;
<p>We can apply this method to the set of list items:</p>
<pre>
  $('li').filter(':even').css('background-color', 'red');
</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>
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;list&lt;/strong&gt; item 1 -
    one strong tag&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;list&lt;/strong&gt; item &lt;strong&gt;2&lt;/strong&gt; -
    two &lt;span&gt;strong tags&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;list item 3&lt;/li&gt;
  &lt;li&gt;list item 4&lt;/li&gt;
  &lt;li&gt;list item 5&lt;/li&gt;
  &lt;li&gt;list item 6&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>We can select the list items, then filter them based on their contents:</p>
<pre>
$('li').filter(function(index) {
  return $('strong', this).length == 1;
}).css('background-color', 'red');
</pre>
<p>This code will alter the first list item only, as it contains exactly one <code>&lt;strong&gt;</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>
$('li').filter(function(index) {
  return index % 3 == 2;
}).css('background-color', 'red');
</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 name="Filtering"/>
<category name="Version 1.0"/>
<category name="Version 1.4"/>
</entry></entries>