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

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


I think you want to use hasClass()

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


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')