I’m working with jQuery and looking to see if there is an easy way to determine if the element has a specific CSS class associated with it.
I have the id of the element, and the CSS class that I’m looking for. I just need to be able to, in an if statement, do a comparison based on the existence of that class on the element.
Use the
hasClassmethod:or
The argument is (obviously) a string representing the class you are checking, and it returns a boolean (so it doesn’t support chaining like most jQuery methods).
Note: If you pass a
classNameargument that contains whitespace, it will be matched literally against the collection’s elements’classNamestring. So if, for instance, you have an element,then this will return
true:and these will return
false: