Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

class

Check if a class active exist on an li with a class menu

For example

<li class="menu active">something...</li> 
like image 500
esafwan Avatar asked Aug 02 '11 11:08

esafwan


People also ask

How check class is active in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How do you check a class exists in a div using jQuery?

The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not. Syntax: $(selector). hasClass(className);


2 Answers

I think you want to use hasClass()

$('li.menu').hasClass('active'); 
like image 123
Richard Dalton Avatar answered Sep 19 '22 18:09

Richard Dalton


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 } 
like image 32
Johan Avatar answered Sep 23 '22 18:09

Johan