Andrew G. Johnson
Andrew G. Johnson

Reputation: 27003

How can I select all elements without a given class in jQuery?

Given the following:

<ul id="list">
    <li>Item 1</li>
    <li class="active">Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
</ul>

How can I select all but Item 2, AKA something like:

$("ul#list li!active")

Upvotes: 256

Views: 185920

Answers (5)

Andre Backlund
Andre Backlund

Reputation: 6953

You can use the .not() method or :not() selector

Code based on your example:

$("ul#list li").not(".active") // not method
$("ul#list li:not(.active)")   // not selector

Upvotes: 487

user3763117
user3763117

Reputation: 323

if (!$(row).hasClass("changed")) {
    // do your stuff
}

Upvotes: 2

Oswaldo Ferreira
Oswaldo Ferreira

Reputation: 1339

You could use this to pick all li elements without class:

$('ul#list li:not([class])')

Upvotes: 21

George Sisco
George Sisco

Reputation: 621

Refer to the jQuery API documentation: not() selector and not equal selector.

Upvotes: 6

Andy Shellam
Andy Shellam

Reputation: 15545

What about $("ul#list li:not(.active)")?

http://api.jquery.com/not-selector/

Upvotes: 43

Related Questions