jQuery: How to check if an element has a particular class

by Yang Yang on April 16, 2009

To know whether a class exists on an element with jQuery, you need a simple test method: is().

For example, to test if an element #elm has the class ‘first’:

if ($(#elm).is('.first')) {
//#elm has the class
} else {
//#elm doesn't have the class

jQuery is() is the function that checks if any of the returned DOM objects from the selector satisfies the criteria set in the argument.