jQuery .filter() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.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)
CallWhat it does
Matched setStarting collection to narrow
Selector / functionWhat to keep (1.0+)
Element / jQueryKeep known nodes (1.4+)
Return valueNew jQuery set (may be empty)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select a broad set (e.g. $("li") or $("div")).
Step 2Call .filter(selector) to keep matching elements.
Step 3Or pass a callback — return true to keep that element.

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
jQuery objectMost 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).

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.

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.

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.

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: .not() Method

Search for descendants inside each element in the matched set.

.not() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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