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>
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>
You could use the jQuery is
function, checking all the classes that you want match.
$("#test").is(".a1,.a2,.a3,.a4,.a5")
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.
You can use the hasClass function.
var test = $('#test');
if(test.hasClass('a1') || test.hasClass('a2') || test.hasClass('a3') ...) {
...
}
if ($("#test").hasClass("a1") || $("#test").hasClass("a2") || $("#test").hasClass("a3") || $("#test").hasClass("a4") || $("#test").hasClass("a5")) {
// Do something
}