JQuery is() and not() and isNot() :not()

print
Probably is() as confusing as it sounds.

Example is();

if($(this).is('.disabled){
    // Do something if the element is disabled
}
else {
    // Do something if the element isn't disabled
}

The assumption then is that the not() function is the opposite.  Sadly, this isn’t the case.  The is() function will return a Boolean value (true/false), however, the not() function is a filter and will return a specific set of elements.

So what is the opposite of is()?  It’s the same as PHP [and others], you just add an ! to reverse it:

if(!$(this).is('.disabled){ 
    // Do something if the element isn't disabled 
} else { 
    // Do something if the element is disabled 
}

 

 

Share this post:
Related Posts
Getting the value or text of an HTML attribute using jQuery
Remove download folders in bulk from Windows terminal server

Leave a Comment