Check if a class `active` exist on element with jquery

You can retrieve all elements having the 'active' class using the following:

$('.active')

Checking wether or not there are any would, i belief, be with

if($('.active').length > 0)
{
    // code
}

You can use the hasClass method, eg.

$('li.menu').hasClass('active') // true|false

Or if you want to select it in one go, you can use:

$('li.menu.active')

$('li.menu.active')

is the simplest way. This will return all elements with both classes.

Or an already answered jQuery hasClass() - check for more than one class


I think you want to use hasClass()

$('li.menu').hasClass('active');

Tags:

Jquery

Class