.filter() (since jQuery 1.0) builds a new set from a subset of the current matched elements. Pass a selector, a function test, a DOM element, or a jQuery object (1.4+). It only looks at elements already in the set — it does not search descendants.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("li").filter(":nth-child(2n)") // even children (official)
$("div").filter(".middle") // keep .middle only
$("li").filter(function (i) { // callback: true → keep
return i % 3 === 2;
});
// filter() → keep matches
// not() → drop matches
// find() → search inside (descendants)
Call
What it does
Matched set
Starting collection to narrow
Selector / function
What to keep (1.0+)
Element / jQuery
Keep known nodes (1.4+)
Return value
New jQuery set (may be empty)
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Select a broad set (e.g. $("li") or $("div")).
Step 2
Call .filter(selector) to keep matching elements.
Step 3
Or pass a callback — return true to keep that element.
Return Value
Return Value
Description
Boolean
Often true or false depending on the match or state.
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
jQuery Examples
Example 1 — Official: .filter(":nth-child(2n)")
Keep even-positioned list items (jQuery API demo).
Modulo.index % 3 === 2 picks every third item starting at index 2.
Frequently Asked Questions
.filter() reduces the current matched set to elements that match a selector, pass a callback test, or match a provided DOM element or jQuery object. It returns a new jQuery object. Available since jQuery 1.0; element/jQuery forms since 1.4.
.filter() keeps matches. .not() drops matches. $("li").filter(".active") keeps active items; $("li").not(".active") keeps the rest.
.filter() only looks at elements already in the set. .find() searches descendants and can return new nodes. Subset vs drill-down.
.filter(function(index, element) { … }) runs once per element. Return true (truthy) to keep it. this is the current DOM element; index is zero-based in the unfiltered set.
Yes, since jQuery 1.4. .filter(element) or .filter($("#unique")) keeps only matching nodes from the current set.
No. It only changes which elements the jQuery object refers to. Chain .css() or .remove() if you need DOM changes.