Reputation: 933
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>
Upvotes: 36
Views: 13215
Reputation: 3056
i just found this thread, and i wanted to clarify that .is()
is very slower than hasClass()
and i found other thread which discusses that.
So, if you care for performance or if you check for large number of elements it is preferred to use hasClass()
also you can check jsperf here.
Upvotes: 1
Reputation: 3147
You could use the jQuery is
function, checking all the classes that you want match.
$("#test").is(".a1,.a2,.a3,.a4,.a5")
Upvotes: 68
Reputation: 5212
if ($("#test").hasClass("a1") || $("#test").hasClass("a2") || $("#test").hasClass("a3") || $("#test").hasClass("a4") || $("#test").hasClass("a5")) {
// Do something
}
Upvotes: 0
Reputation: 16974
You can use the hasClass function.
var test = $('#test');
if(test.hasClass('a1') || test.hasClass('a2') || test.hasClass('a3') ...) {
...
}
Upvotes: 1