Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How to check if element has any of these classes

Tags:

html

jquery

Example:

How do I check if div #test has any of .a1 .a2 .a3 .a4 .a5 classes? With only one if-statment...

<div id="test" class="a1 a2 a5"></div> 
like image 436
galengodis Avatar asked Nov 08 '11 11:11

galengodis


People also ask

How can check CSS property value in jQuery?

Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");

How do you find the class of a clicked element?

To find the class of clicked element, we use this. className property. The className property is used to set or return the value of an element's class attribute. Using this property, the user can change the class of an element to the desired class.


2 Answers

You could use the jQuery is function, checking all the classes that you want match.

$("#test").is(".a1,.a2,.a3,.a4,.a5") 
like image 108
Ferran Basora Avatar answered Sep 24 '22 21:09

Ferran Basora


You can use the hasClass function.

var test = $('#test'); if(test.hasClass('a1') || test.hasClass('a2') || test.hasClass('a3') ...) { ... } 
like image 45
Andy Rose Avatar answered Sep 20 '22 21:09

Andy Rose