jQuery .filter() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is .filter()?

.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)
NeedUse
Keep by selector$set.filter(".keep")
Drop matches instead.not()
Search inside.find()
Boolean test.is()
Custom rule.filter(function (i, el) { … })

How to Use .filter()

Start broad, then keep only what you need.

  1. Select a broad set (e.g. $("li") or $("div")).
  2. Call .filter(selector) to keep matching elements.
  3. Or pass a callback — return true to keep that element.
  4. Or pass a DOM node / jQuery object (1.4+) to keep those nodes.
  5. Chain styling or other methods on the reduced set.
PieceRequired?Role
Matched setYesStarting collection to narrow
Selector / functionOne formWhat to keep (1.0+)
Element / jQueryOne formKeep 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

Cheat Sheet

Keep filtering methods side by side.

TopicDetail
Syntax.filter(sel|fn|element|jQuery)
Since1.0 · element & jQuery in 1.4
vs .not()Keep matches vs drop matches
Callback trueMeans keep
vs .find()Subset current vs search descendants
vs .is()New set vs boolean
DOM change?No — query only

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.

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).

jQuery
$("li").filter(":nth-child(2n)").append(" [kept]");
Try It Yourself

How It Works

  1. Select. Start with every li.
  2. Test. :nth-child(2n) matches even children.
  3. Keep. Items 2, 4, and 6 remain.
  4. Mark. Only those three get the suffix.

Example 2 — Official: Style All, Then Filter .middle

Act on the full set first; then narrow for a second step.

jQuery
$("div")
  .append(" [all]")
  .filter(".middle")
  .append(" [middle]");
Try It Yourself

How It Works

  1. Broad. Select all div elements.
  2. First mark. Every div gets [all].
  3. Filter. Keep only .middle divs.
  4. Second mark. Only that subset gets [middle].

Example 3 — Callback: true Means Keep

Official idea: use index or contents to decide.

jQuery
$("li").filter(function (index) {
  return index % 3 === 2;   // keep indexes 2, 5, …
}).append(" [kept]");
Try It Yourself

How It Works

  1. Callback. Runs once per element with index.
  2. True. Returning truthy keeps that element.
  3. Modulo. index % 3 === 2 picks every third item starting at index 2.
  4. Opposite. In .not(), true would mean exclude.

Example 4 — .filter() vs .not()

Same class — opposite result sets.

jQuery
$("#a li").filter(".active").append(" [filter]");
$("#b li").not(".active").append(" [not]");
Try It Yourself

How It Works

  1. filter. Keeps .active only.
  2. not. Drops .active — inactive items remain.
  3. Same class. Opposite collections from one rule.
  4. 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]");
Try It Yourself

How It Works

  1. Broad set. Select all div elements.
  2. Reference. Pass one DOM node (or a jQuery object).
  3. Keep. Only that node remains if it was in the set.
  4. 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.

Performance Notes

ApproachCostBest for
.filter(selector)CheapKeep by class / id / pseudo
.filter(element)CheapestKeep one known node
.filter(fn)Per-element callCustom rules
Narrower start setFasterPrefer $("ul li") over $("*")

Prefer a tight starting selector, then .filter(), over selecting everything and filtering a huge set.

Key Takeaways

  • Role: keep matching elements from the current set.
  • vs not: keep matches vs drop matches.
  • Callback: return true to keep.
  • Next: .find() searches descendants.

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.

Next: .find() Method

Search for descendants inside each element in the matched set.

.find() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful