Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Check if multiple elements have a class

Tags:

html

jquery

I need to run a function inside a conditional statement based on whether or not 3 elements have a class.

In short, if (elm1, elm2, elm3) have the class "include" then ring true. But all 3 elements must have the class, it can not ring true if only 2 or 1 has the class.

<div class="elm1" class="include"></div>
<div class="elm2" class="include"></div>
<div class="elm3" class="include"></div>

jQuery('#search-submit').click(function() { 
    if (#elm1, #elm2, #elm3).hasClass('include'){
        /* return true */
    } else {
        /* return false */
    }
}

Needless to say this doesn't work.

like image 973
Alex Knopp Avatar asked Dec 10 '25 09:12

Alex Knopp


1 Answers

You can do something like

<div class="elm1 include"></div>
<div class="elm2 include"></div>
<div class="elm3 include"></div>

 var classNotFound = false;
 $('.elm1, .elm2, .elm3').each(function() {
     if (!$(this).hasClass('include')){
        classNotFound = true;
     }
 });
 return classNotFound;
like image 78
dchar Avatar answered Dec 11 '25 22:12

dchar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!