.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.
Remember
$("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)
Or pass a callback — return true to keep that element.
Or pass a DOM node / jQuery object (1.4+) to keep those nodes.
Chain styling or other methods on the reduced set.
Piece
Required?
Role
Matched set
Yes
Starting collection to narrow
Selector / function
One form
What to keep (1.0+)
Element / jQuery
One form
Keep known nodes (1.4+)
Return value
—
New jQuery set (may be empty)
Pseudocode
Pseudocode
call $(set).filter(criterion)
result = empty set
for each element in set (with index i):
if criterion is a function:
keep = criterion(i, element) is truthy
else if criterion is a selector:
keep = element matches selector
else:
keep = element is in criterion (node / jQuery)
if keep:
add element to result
return result as a new jQuery object
Reference
Cheat Sheet
Keep filtering methods side by side.
Topic
Detail
Syntax
.filter(sel|fn|element|jQuery)
Since
1.0 · element & jQuery in 1.4
vs .not()
Keep matches vs drop matches
Callback true
Means keep
vs .find()
Subset current vs search descendants
vs .is()
New set vs boolean
DOM change?
No — query only
Try it
Live Preview
From six list items, compare selector filter, callback filter, and .not().
list item 1
list item 2
list item 3
list item 4
list item 5
list item 6
Official idea: .filter(":nth-child(2n)") keeps items 2, 4, and 6.
Result
Ready — choose a call and click Run.
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.
Example 1 — Official: .filter(":nth-child(2n)")
Keep even-positioned list items (jQuery API demo).
.filter(".active") → active items [filter]
.not(".active") → inactive items [not]
How It Works
filter. Keeps .active only.
not. Drops .active — inactive items remain.
Same class. Opposite collections from one rule.
Pick. Use filter to keep; not to exclude.
Example 5 — Filter by DOM Node or jQuery Object
Official idea: keep only a known element from the set.
jQuery
$("div").filter(document.getElementById("unique")).append(" [kept]");
// Same idea with a jQuery object:
// $("div").filter($("#unique")).append(" [kept]");
Reference. Pass one DOM node (or a jQuery object).
Keep. Only that node remains if it was in the set.
Use. Handy when another API already gave you a node.
Edge Cases & Pitfalls
Why a filtered set can surprise you.
callback
Callback true keeps
Opposite of .not(). Returning true means “include this one.”
find
Does not search descendants
.filter() only tests the current set. Use .find() to look inside.
nodes
Selector form drops text/comment nodes
Official note: a CSS selector string strips text and comment nodes during filtering.
empty
No matches → empty set
If nothing passes the test, .length is 0. Check length when that matters.
Compatibility
Browser Support
.filter() has been part of jQuery since 1.0. Filtering by DOM element or jQuery object arrived in 1.4. It works in every browser that runs your loaded jQuery version.
✓ Stable · jQuery 1.0+
jQuery .filter()
Supported across jQuery 1.x, 2.x, and 3.x. Prefer a current jQuery 3.x build. Filtering operates on the current matched set only.
100%With jQuery
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.filter()Universal
Bottom line: Safe for narrowing by class, pseudo, or custom test. Remember: callback true means keep; use .find() for descendants.
Analysis
Performance Notes
Approach
Cost
Best for
.filter(selector)
Cheap
Keep by class / id / pseudo
.filter(element)
Cheapest
Keep one known node
.filter(fn)
Per-element call
Custom rules
Narrower start set
Faster
Prefer $("ul li") over $("*")
Prefer a tight starting selector, then .filter(), over selecting everything and filtering a huge set.
Remember
Key Takeaways
Role: keep matching elements from the current set.
One line:.filter() keeps what matches — the inverse of .not(), and not a descendant search.
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.