jQuery .not() Method

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

What Is .not()?

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

Remember
$("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
NeedUse
Exclude by selector$set.not(".skip")
Keep matches instead.filter()
Boolean test.is()
Exclude one DOM node.not(element)
Custom rule.not(function (i, el) { … })

How to Use .not()

Start with a set, then drop what you do not want.

  1. Select a broad set (e.g. $("li") or $("div")).
  2. Call .not(selector) to remove matching elements.
  3. Or pass a DOM node / jQuery object / array (1.4+ for selection & function).
  4. Or use a callback — return true to exclude that element.
  5. Chain styling or other methods on the remaining set.
PieceRequired?Role
Matched setYesStarting collection to narrow
Selector / element / arrayOne formWhat to remove (1.0+)
Function / jQuery selectionOne formCustom test or known set (1.4+)
Return value—New jQuery set (may be empty)

Pseudocode

Pseudocode
call $(set).not(criterion)

result = empty set
for each element in set (with index i):
    if criterion is a function:
        exclude = criterion(i, element) is true
    else if criterion is a selector:
        exclude = element matches selector
    else:
        exclude = element is in criterion (node / array / jQuery)

    if NOT exclude:
        add element to result

return result as a new jQuery object

Cheat Sheet

Keep filtering methods side by side.

TopicDetail
Syntax.not(selector|fn|element|jQuery)
Since1.0 · function & jQuery in 1.4
vs .filter()Drop matches vs keep matches
Callback trueMeans exclude
vs .is()New set vs boolean
Official list demo$("li").not(":nth-child(2n)")
DOM change?No — query only

Live Preview

From five list items, compare .not(), .filter(), and a callback exclude.

  • list item 1
  • list item 2
  • list item 3
  • list item 4
  • list item 5

Official idea: .not(":nth-child(2n)") keeps items 1, 3, 5.

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: $("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.
  4. Keep. Items 1, 3, and 5 remain and get marked.

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.
  4. Mark. Only the remaining divs get the suffix.

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.
  4. Pick. Use not to exclude; filter to keep.

Example 4 — Callback: true Means Exclude

Drop the first two items by index.

jQuery
$("li").not(function (index) {
  return index < 2;   // true → exclude items 0 and 1
}).append(" [kept]");
Try It Yourself

How It Works

  1. Callback. Runs once per element with index.
  2. True. Returning true excludes that element.
  3. False. Returning false keeps it.
  4. Opposite. In .filter(), true would mean keep.

Example 5 — Exclude a DOM Node or jQuery Object

Official idea: remove one known element from the set.

jQuery
$("li").not(document.getElementById("notli")).append(" [kept]");

// Same idea with a jQuery object:
// $("li").not($("#notli")).append(" [kept]");
Try It Yourself

How It Works

  1. Reference. Get the third item as a plain DOM node.
  2. Exclude. .not(node) drops that node from the set.
  3. Rest. Items 1, 2, 4, and 5 remain.
  4. Use. Handy when another library hands you a DOM reference.

Edge Cases & Pitfalls

Why an excluded set can surprise you.

callback

Callback true excludes

Opposite of .filter(). Returning true means “remove this one.”

dom

Does not remove from the page

.not() only shrinks the jQuery collection. Chain .remove() if you need DOM deletion.

nodes

Selector form drops text/comment nodes

Official note: a CSS selector string always strips text and comment nodes from the result during filtering.

empty

Everything matched → empty set

If every element matches the criterion, .length is 0. Check length when that matters.

Browser Support

.not() has been part of jQuery since 1.0. The function and jQuery-object forms were added in 1.4. It works in every browser that runs your loaded jQuery version.

✓ Stable · jQuery 1.0+

jQuery .not()

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

Bottom line: Safe for excluding classes, ids, or known nodes from a collection. Remember: callback true means exclude.

Performance Notes

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

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

Key Takeaways

  • Role: remove matching elements from the current set.
  • vs filter: drop matches vs keep matches.
  • Callback: return true to exclude (1.4+).
  • DOM: collection only — not page removal.

One line: .not() keeps everything that does not match — the inverse of .filter().

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

Select odd-indexed elements from the current matched set.

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

10 people found this page helpful