jQuery .not() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.not(selector|element|array) (since jQuery 1.0) and .not(function|selection) (since 1.4) build a new set without the rejected elements. Pass a selector, DOM node, array, jQuery object, or callback. Returns a new jQuery object; does not change the DOM by itself.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("li").not(":nth-child(2n)");     // odd items (official)
$("div").not(".green, #blueone");  // exclude green + #blueone
$("p").not("#selected");           // drop one id
$("li").not(function (i) {         // callback: true → exclude
  return i % 2 === 0;
});

// filter() → keep matches
// not()    → drop matches
CallWhat it does
Matched setStarting collection to narrow
Selector / element / arrayWhat to remove (1.0+)
Function / jQuery selectionCustom test or known set (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 .not(selector) to remove matching elements.
Step 3Or pass a DOM node / jQuery object / array (1.4+ for selection & function).

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: $("li").not(":nth-child(2n)")

Keep odd-positioned list items (jQuery API demo).

jQuery
$("li").not(":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. Drop. Even items leave the set.

Example 2 — Official: Exclude .green and #blueone

Mark every div that is not green or blue (jQuery API demo).

jQuery
$("div").not(".green, #blueone").append(" [border]");
Try It Yourself

How It Works

  1. Broad set. Select all div elements.
  2. Exclude. Drop anything matching .green or #blueone.
  3. Comma. One .not() can list several selectors.

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

Same test — opposite result sets.

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

How It Works

  1. not. Drops .active — inactive items remain.
  2. filter. Keeps .active only.
  3. Same class. Opposite collections from one rule.

Frequently Asked Questions

.not() removes elements from the current jQuery collection — it returns a new jQuery object containing only elements that do NOT match a selector, fail a callback test, or match a provided DOM element or jQuery object. It is the inverse of .filter().
.filter() keeps elements that match. .not() keeps elements that do not match. $("li").filter(".active") returns active items; $("li").not(".active") returns inactive ones.
.is() returns a boolean — true if at least one element matches. .not() returns a new jQuery object with matching elements removed. Use .is() to test; use .not() to exclude and keep chaining.
Yes, since jQuery 1.4. .not(domElement) removes that node if present. .not($("#selected")) removes elements that match nodes in the jQuery object.
.not(function(index, element) { ... }) runs once per element. If the function returns true, that element is excluded. Inside the callback, this refers to the current DOM element — opposite of .filter(), where true means keep.
No. .not() only changes which elements the jQuery object refers to. It does not remove nodes from the page unless you chain something like .remove() afterward.

Next: .first() Method

Select odd-indexed elements from the current matched set.

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

10 people found this page helpful