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

Jquery

Jquery Problem Overview


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")

Jquery Solutions


Solution 1 - Jquery

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

Solution 2 - Jquery

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

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

Solution 3 - Jquery

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

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

Solution 4 - Jquery

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

Solution 5 - Jquery

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew G. JohnsonView Question on Stackoverflow
Solution 1 - JqueryAndre BacklundView Answer on Stackoverflow
Solution 2 - JqueryAndy ShellamView Answer on Stackoverflow
Solution 3 - JqueryOswaldo FerreiraView Answer on Stackoverflow
Solution 4 - JqueryGeorge SiscoView Answer on Stackoverflow
Solution 5 - Jqueryuser3763117View Answer on Stackoverflow