Tried this but it doesn't seem to be working
$('this:not([class]="doneAnimated"'){
}
HTML
<div class="doneAnimated">
</div>
<div class="doneAnimated">
</div>
<div class="">
</div>
I want to select only div that doesn't have .doneAnimated.
Tried this but it doesn't seem to be working
$('this:not([class]="doneAnimated"'){
}
HTML
<div class="doneAnimated">
</div>
<div class="doneAnimated">
</div>
<div class="">
</div>
I want to select only div that doesn't have .doneAnimated.
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.
$("div:not(.doneAnimated)")
is what you are looking for. The :not()
function can contain any CSS selector, so insert .doneAnimated
into it.
Here is a working example
HTML
<div class="doneAnimated" style="display:none">
has doneAnimated class 1
</div>
<div class="doneAnimated" style="display:none">
has doneAnimated class 2
</div>
<div class="" style="display:none">
no doneAnimated class 1
</div>
<div class="randomClass" style="display:none">
no doneAnimated class 2
</div>
JS
$.each($("div:not(.doneAnimated)"), function() {
$(this).show();
});