Not class selector in jQuery
You need the :not()
selector:
$('div[class^="first-"]:not(.first-bar)')
or, alternatively, the .not()
method:
$('div[class^="first-"]').not('.first-bar');
How to write if not class jQuery selector?
var p = $("li:last").not(".Class");
http://api.jquery.com/not/
jQuery select elements that do not contain a class
To do it just with a selector, you'd use :not
with :has
:
$(".hi:not(:has(.image))")
// or to be more specific:
$(".hi:not(:has(.hue > .image))")
Note that :has
is jQuery-specific.
To do it with filter
, you could use find
in the callback:
$(".hi").filter(function() { return $(this).find(".image").length == 0; })
// or to be more specific:
$(".hi").filter(function() { return $(this).find(".hue > .image").length == 0; })
How can I select all elements without a given class in 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
If not class clicked
If it's supposed to work for all div elements, target all div elements (or whatever you like really), and check if it has the class or not inside the event handler :
$('div').on('click', function() {
if ( $(this).hasClass('whatever') ) {
alert("You clicked a div with the class");
} else {
alert("You clicked a div without the class");
}
});
How to ignore multiple classes using the :not selector?
The :not selector can take multiple CSS selectors as arguments ( http://api.jquery.com/not-selector ). E.g.
$('div:not(.a,.b)').empty()
http://jsfiddle.net/HFfcP/
Has no class name jquery selector
How about
$("div:not(.doneAnimated)")
Looking at the class property directly is a fragile programming practice. The class is a list, so checking for simple equality doesn't always work — and if it works today, it may not work after later changes.
If you need to check when your jQuery object is not formed by a selector, you can use .not()
:
$(this).not(".doneAnimated")
If you want to just test the state of this
:
if (!$(this).is(".doneAnimated")) {
// do something
}
The .is()
method returns true
or false
, while .not()
acts like a filter on the jQuery object contents.
edit a comment points out that an alternative to the general-case .is()
is .hasClass()
:
if (!$(this).hasClass("doneAnimated")) {
// do something
}
Note that with .hasClass()
you pass in just the class name, without a leading .
, a mistake I've made more than once.
How to Select Element That Does Not have Specific Class
This selects the second LI
element.
document.querySelector("li:not([class])")
or
document.querySelector("li:not(.completed):not(.selected)")
Example:
// select li which doesn't have a 'class' attribute...console.log(document.querySelector("li:not([class])"))
// select li which doesn't have a '.completed' and a '.selected' class...console.log(document.querySelector("li:not(.completed):not(.selected)"))
<ul id="tasks"> <li class="completed selected">One Task</li> <li>Two Task</li> </ul>
select input type next with not class selector
You do not have a text
class assigned to your input element.
You can use the attribute selector:
$('input[type="text"]:not(.fld-required-' + val + ')').each(function() {
$(this).val('');
});
JQuery : Select an element with N clases and not class X
You can use selector like this $('div.a.b:not(.c)')
div.a.b
selects alldiv
elements that have both classesa
andb
:not(.c)
but won't selectdiv
element with classc
$('div.a.b:not(.c)').css('color', 'red')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div class='a b c'>Div</div><div class='a b'>Div</div>
Related Topics
Javascript: Call a Function After Specific Time Period
How to Remove a Class from Elements in Pure JavaScript
How to Round Numbers into a Reader Friendly Format? (E.G. $1.1K)
How to Make a Div Element Editable (Like a Textarea When I Click It)
Reactjs: Prevent Multiple Times Button Press
Can Read-Only Properties Be Implemented in Pure JavaScript
Ajax Call and Clean JSON But Syntax Error: Missing ; Before Statement
Reactjs Syntheticevent Stoppropagation() Only Works with React Events
Puppeteer - Scroll Down Until You Can't Anymore
Is Performing a Mapping Operation Without Using Returned Value an Antipattern
Advanced JavaScript: Why Is This Function Wrapped in Parentheses
JavaScript Library for Human-Friendly Relative Date Formatting
Get Decimal Portion of a Number with JavaScript
Replacing &Nbsp; from JavaScript Dom Text Node