Description: Reduce the set of matched elements to those that match the selector or pass the function's test.
.filter( selector )
.filter( function(index) )
this is the current DOM element.
.filter( element )
.filter( jQuery object )
Given a jQuery object that represents a set of DOM elements, the .filter() 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.
Consider a page with a simple list on it:
<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>
</ul>
$('li').filter(':even').css('background-color', 'red');
The result of this call is a red background for items 1, 3, and 5, as they match the selector (recall that :even and :odd use 0-based indexing).
- list item 1
- list item 2
- list item 3
- list item 4
- list item 5
Using a Filter Function
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 true, the element will be included in the filtered set; otherwise, it will be excluded. Suppose we have a somewhat more involved HTML snippet:
<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>
This code will alter the first list item only, as it contains exactly one <strong> tag. Within the filter function, this 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.
We can also take advantage of the index passed through the function, which indicates the 0-based position of the element within the unfiltered set of matched elements:
$('li').filter(function(index) {
return index % 3 == 2;
}).css('background-color', 'red');
- list item 1 - one strong tag
- list item 2 - two strong tags
- list item 3
- list item 4
- list item 5
- list item 6
This alteration to the code will cause the third and sixth list items to be highlighted, as it uses the modulus operator (%) to select every item with an index value that, when divided by 3, has a remainder of 2.
Examples
Example 1
Change the color of all divs then put a border around only some of them.Example 1 - Full source:
Change the color of all divs then put a border around only some of them.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("div").css("background", "#c8ebcc")
.filter(".middle")
.css("border-color", "red");
});
</script>
<style>
div { width:60px; height:60px; margin:5px; float:left;
border:2px white solid;}
</style>
</head>
<body>
<div></div>
<div class="middle"></div>
<div class="middle"></div>
<div class="middle"></div>
<div class="middle"></div>
<div></div>
</body>
</html>
Example 2
Selects all paragraphs and removes those without a class "selected".$("p").filter(".selected")
Example 3
Selects all paragraphs and removes those that aren't of class "selected" or the first one.$("p").filter(".selected, :first")
Example 4
Change the color of all divs then put a border to specific ones.Example 4 - Full source:
Change the color of all divs then put a border to specific ones.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("div").css("background", "#b4b0da")
.filter(function (index) {
return index == 1 || $(this).attr("id") == "fourth";
})
.css("border", "3px double red");
});
</script>
<style>
div { width:60px; height:60px; margin:5px; float:left;
border:3px white solid; }
</style>
</head>
<body>
<div id="first"></div>
<div id="second"></div>
<div id="third"></div>
<div id="fourth"></div>
<div id="fifth"></div>
<div id="sixth"></div>
</body>
</html>
Example 5
Remove all elements that have a descendant ol element$("div").filter(function(index) {
return $("ol", this).length == 0;
});

