How do I check if there isn't a class. For example, I know how to check to see if it has the class "test", but how do I check to see if it doesn't have the class "test"?
if($(this).hasClass("test")){
}
How do I check if there isn't a class. For example, I know how to check to see if it has the class "test", but how do I check to see if it doesn't have the class "test"?
if($(this).hasClass("test")){
}
use the .not() method and check for an attribute:
Check it here: http://jsfiddle.net/AWb79/
Select element (or group of elements) having class "abc", not having class "xyz":
When selecting regular CSS you can use
.abc:not(.xyz)
.There are more complex scenarios where this doesn't work. What if you want to select an element with class A that doesn't contain elements with class B. You end up needing something more like:
If parent element does not contain certain child element; jQuery
reading through this 6yrs later and thought I'd also take a hack at it, also in the TIMTOWTDI vein...:D, hoping it isn't incorrect 'JS etiquette'.
I usually set up a var with the condition and then refer to it later on..i.e;
Although I should mention that I do this because I mainly use the conditional ternary operator and want clean code. So in this case, all i'd have is this:
...instead of this:
You can try this: