I'm trying to have something happen on every instance except where there is a disabled class, but .not() doesn't seem to work. Here is a short version of what I have:
if($(this).not('.Disabled')){
-Do Something-
}
However, if I reverse it, i.e.
if($(this).is('.Disabled')){
-Do Something-
}
then the "is" will work on an element with the class of "Disabled" Kind of confused where to go from here.
The .not
method returns a jQuery object containing the elements in this set that don't match the selector.
It doesn't return a boolean.
In other words, it's the opposite of .filter
, not .is
.
You could write if ($(this).not('.Disabled').length)
to check whether the jQuery object it creates is empty.
However, you should use the unary negation operator: if (!something)
You want to use hasClass
to test if an element has a class:
if($(this).hasClass("disabled")) {
// i am disabled
}
If you're talking about the disabled attribute, then use the :disabled
selector:
if($(this).is(":disabled")) {
// i am disabled
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With