2

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
jamie eason
  • 141
  • 10

4 Answers4

4

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0
$('div').not('.doneAnimated');
Andrew Briggs
  • 1,329
  • 12
  • 26
0

$("div:not(.doneAnimated)") is what you are looking for. The :not() function can contain any CSS selector, so insert .doneAnimated into it.

Ian Hazzard
  • 7,661
  • 7
  • 34
  • 60
  • 1
    Note that the definition of "CSS selector", or "selector" for that matter, for the purposes of `:not()` varies depending on whether you're using CSS or jQuery. jQuery is much more permissive, CSS is much more restrictive. See http://stackoverflow.com/questions/10711730/why-is-my-jquery-not-selector-not-working-in-css – BoltClock Dec 31 '14 at 02:23
0

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();
});

FIDDLE

Lumi Lu
  • 3,289
  • 1
  • 11
  • 21