Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery .not() not working

Tags:

jquery

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.

like image 803
Tami Avatar asked Dec 28 '22 22:12

Tami


2 Answers

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)

like image 125
SLaks Avatar answered Jan 04 '23 22:01

SLaks


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
}
like image 23
karim79 Avatar answered Jan 05 '23 00:01

karim79