jQuery :not() Selector

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Negation filter

What You’ll Learn

The :not() selector is jQuery’s negation filter: it keeps every element that does not match the selector inside the parentheses. Available since jQuery 1.0, it is ideal for “everything except…” queries — unchecked inputs, inactive menu items, or visible fields minus hidden tokens.

01

Syntax

:not(selector)

02

Exclude

Skip matched nodes

03

Classes

:not(.active)

04

Official

input:not(:checked)

05

Lists

:not(.a, .b)

06

.not()

Method alternative

Introduction

Most jQuery selectors answer “give me elements that look like this.” The :not() pseudo-class flips the question: “give me elements that do not look like this.” You attach it to a base selector — for example input:not(:checked) means “every input except the checked ones.”

This pattern shows up constantly in UI work: dim inactive tabs, style unchecked labels, skip hidden CSRF fields, or highlight published cards while ignoring drafts. Official jQuery documentation also notes that the .not() method is often easier to read when exclusions get complex — both approaches are covered here.

Understanding the :not() Selector

Think of :not() as a gate at the end of a selector. jQuery first finds candidates matching the part before :not(), then removes any that also match the filter inside the parentheses.

  • li:not(.active) → all li elements except those with class active.
  • input:not(:checked) → inputs that are not currently checked.
  • div:not(.sidebar) → every div except sidebar blocks.
  • div:not(.skip, .draft) → exclude two classes in one expression.
💡
Beginner Tip

Read :not() aloud: “not this.” The base selector sets the pool; the inner selector defines what to subtract. Start with a simple class exclusion like :not(.hidden) before combining pseudo-classes.

📝 Syntax

Official jQuery API form (since 1.0):

jQuery
jQuery( ":not(selector)" )

// common patterns:
$( "li:not(.active)" )
$( "input:not(:checked)" )
$( "input:not([type=hidden])" )
$( "div:not(.skip, .draft)" )

// complex inner filters (official docs):
$( "div:not(div a)" )   // exclude divs containing an anchor
$( "div:not(div,a)" )   // comma-separated exclusions

Parameters

  • selector — any valid jQuery/CSS selector (or comma-separated list) describing elements to exclude from the result.

Return value

  • A jQuery object containing every element that matched the base selector and did not match the inner filter.
  • An empty collection when every candidate is excluded or none exist.

Official jQuery API example

jQuery
// Highlight span labels beside unchecked inputs
$( "input:not(:checked) + span" ).css( "background-color", "yellow" );

// Disable inputs so the demo state stays fixed
$( "input" ).attr( "disabled", "disabled" );

⚡ Quick Reference

GoalSelector
Exclude a classli:not(.active)
Exclude checked inputsinput:not(:checked)
Exclude hidden fieldsinput:not([type=hidden])
Exclude multiple classesdiv:not(.a, .b)
Method alternative$("li").not(".active")

📋 :not() vs .not() Method

Both remove matches — choose based on readability and whether you already hold a collection.

:not()
button:not(.ghost)

CSS selector filter

.not()
$("button").not(".ghost")

Traverse existing set

Dynamic
.not(myVariable)

Variables & functions

Official tip
Prefer .not()

When complex

Examples Gallery

Example 1 follows the official jQuery demo. Examples 2–5 cover class exclusions, attribute filters, comma-separated lists, and the .not() method. Use the Try-it links to run each snippet in the browser.

📚 Official jQuery Demo

Highlight labels beside unchecked inputs — the canonical :not() example from the API docs.

Example 1 — Official Demo: input:not(:checked) + span

Find every input that is not checked, then style the adjacent span sibling yellow. Inputs are disabled so the demo state stays visible.

jQuery
$( "input:not(:checked) + span" ).css( "background-color", "yellow" );

// Keep demo state fixed (official demo disables inputs)
$( "input" ).attr( "disabled", "disabled" );
Try It Yourself

How It Works

:not(:checked) removes checked boxes from the input pool. The adjacent sibling combinator + span then targets only the label beside each remaining unchecked input.

📈 Practical Patterns

Classes, attributes, lists, and the traversal method.

Example 2 — Exclude Active Tab: li:not(.active)

Dim every navigation item except the currently active one.

jQuery
$( "#nav li:not(.active)" ).addClass( "dimmed" );

// Only non-active items receive the dimmed class
Try It Yourself

How It Works

The base selector li gathers every item; :not(.active) subtracts the highlighted tab. This is one of the most common beginner uses of negation.

Example 3 — Skip Hidden Fields: input:not([type=hidden])

Count or validate only visible form inputs, excluding CSRF tokens and other hidden values.

jQuery
var visible = $( "#signup input:not([type=hidden])" ).length;

$( "#count" ).text( visible + " visible input(s)" );

// Hidden csrf field is excluded from the count
Try It Yourself

How It Works

Attribute selectors work inside :not(). The hidden CSRF input matches [type=hidden], so it is removed from the counted set.

Example 4 — Multiple Exclusions: div:not(.skip, .draft)

Official jQuery docs accept comma-separated selectors inside :not() — exclude archived and draft cards in one expression.

jQuery
$( "div.card:not(.skip, .draft)" ).addClass( "highlight" );

// Equivalent idea with chained :not() — less readable:
// $("div.card").not(".skip").not(".draft")
Try It Yourself

How It Works

Each comma-separated selector inside the parentheses is treated as an independent exclusion rule. Elements matching any listed selector are removed.

Example 5 — When to Use .not() Instead

Official docs recommend .not() for readability when filters involve variables or multiple exclusions on an existing collection.

jQuery
// CSS :not() — compact for simple cases
$( "#toolbar button:not(.ghost)" ).addClass( "via-not" );

// .not() — clearer when excluding several classes
$( "#toolbar button" ).not( ".ghost, .danger" ).addClass( "via-method" );

// .not() also accepts a jQuery object or function filter
Try It Yourself

How It Works

Both approaches subtract matches, but .not() runs on a collection you already selected — ideal for chaining and dynamic values. Keep :not() for straightforward one-line selectors.

🚀 Common Use Cases

  • Form styling — highlight labels beside input:not(:checked).
  • Navigation — dim li:not(.active) menu items.
  • Validation — operate on visible fields with input:not([type=hidden]).
  • Content filters — show published items via div:not(.draft).
  • Table rows — select tr:not(.empty-row) for bulk actions.
  • Refactoring — replace long negative logic with a readable .not() chain.

🧠 How jQuery Evaluates :not()

1

Parse selector

Split the string into a base part (e.g. input) and the :not(...) filter.

Syntax
2

Find candidates

Collect every element matching the base selector within the search context.

Query
3

Apply negation

Remove any candidate that also matches the inner selector (or any comma-separated exclusion).

Filter
4

Return collection

Remaining elements become a jQuery object ready for .css(), .addClass(), or events.

📝 Notes

  • Available since jQuery 1.0 — one of the oldest jQuery selector extensions built on standard CSS negation.
  • All selector types are accepted inside the parentheses, including combinators and nested pseudo-classes.
  • Official docs: .not() often produces more readable code than deeply nested :not() strings.
  • Comma-separated lists inside :not() exclude multiple patterns in one pass (:not(.a, .b)).
  • Scope matters — $("#form input:not(:checked)") only searches inside the form.
  • Native document.querySelectorAll supports :not() in modern browsers; jQuery adds consistent behavior across older targets.

Browser Support

The :not() pseudo-class is part of CSS3 and works in jQuery 1.0+. Modern browsers support negation in native querySelectorAll; jQuery’s Sizzle engine handles comma-separated exclusions consistently.

CSS3 · jQuery 1.0+

jQuery :not() Selector

Supported in all modern browsers. Native: document.querySelectorAll("li:not(.active)"). jQuery extends negation with comma-separated lists inside :not().

100% Standard + 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 modern apps. Prefer .not() when exclusions involve variables, functions, or jQuery objects — official docs recommend it for complex filters.

Conclusion

The :not() selector answers exclusion questions in a single CSS-style expression — unchecked inputs, inactive tabs, visible fields minus hidden tokens. The official input:not(:checked) + span demo shows how negation pairs naturally with sibling combinators.

Start with simple class filters like :not(.hidden), then reach for .not() when the logic grows beyond what you want to cram into one selector string.

💡 Best Practices

✅ Do

  • Scope with a parent ID: $("#form input:not(:checked)")
  • Use :not(.class) for simple single exclusions
  • Switch to .not() when filters get complex
  • Combine with other pseudo-classes: input:not(:checked)
  • Test Try-it labs to see live DOM filtering

❌ Don’t

  • Stack many nested :not() calls — use .not()
  • Forget the base selector — bare :not(.x) is invalid alone
  • Assume :not() mutates the DOM — it only filters results
  • Confuse :not(.a .b) (descendant inside) with :not(.a, .b) (two exclusions)
  • Overuse negation when a positive selector would be clearer

Key Takeaways

Knowledge Unlocked

Five things to remember about :not()

Exclude matches cleanly in jQuery selectors.

5
Core concepts
! 02

Exclude

Subtract matches

Logic
, 03

Lists

:not(.a, .b)

Multi
.not 04

Method

When complex

Tip
05

Official

unchecked + span

Demo

❓ Frequently Asked Questions

:not(selector) matches every element in the search context that does NOT satisfy the inner selector. $("li:not(.active)") returns all list items except those with class active. It is a filter you attach to a base selector.
jQuery accepts any valid selector inside the parentheses — element types, classes, IDs, attributes, and other pseudo-classes. Official docs show both :not(div a) for complex filters and :not(div,a) for comma-separated exclusions.
:not() is part of a CSS selector string evaluated when jQuery queries the DOM. .not() is a jQuery method that removes items from an existing collection. For complex exclusions, .not() is often more readable than nesting long strings inside :not().
Yes. Use a comma-separated list inside the parentheses: $("div:not(.skip, .draft)"). Each comma-separated selector is excluded independently.
Yes. CSS3 defines :not() and modern browsers support it in querySelectorAll. jQuery has supported :not() since version 1.0 and extends it with comma-separated lists in some cases beyond early CSS levels.
Prefer .not() when you already have a jQuery collection, when the exclusion involves a variable or dynamic filter, or when the negation logic would make a single selector string hard to read. Official jQuery docs recommend .not() for readability in most complex cases.
Did you know?

Official jQuery documentation states that the .not() method usually produces more readable selections than pushing complex variables into a :not() filter. Both subtract matches — choose the form that your teammates can scan fastest.

Continue to Child Selector (>)

After negation filters, learn how the child combinator matches direct descendants only.

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

6 people found this page helpful