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.
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
className argument that contains whitespace, it will be matched literally against the collection's elements'
className string. So if, for instance, you have an element,
<span class="foo bar" />
then this will return
and these will return
$('span').hasClass('bar foo') $('span').hasClass('foo bar')