vendredi 22 mai 2020

jquery check if any element of collection / array has Class

I'm trying to simplify a jquery code. I have a selection of elements, and I want to check if ANY of those elements has a specific class, same specific class for all selectors.

if($(".sf-item-352").hasClass("sf-option-active") || $(".sf-item-356").hasClass("sf-option-active") || $(".sf-item-362").hasClass("sf-option-active")) {

     do_something();

} else {

     do_something_else();

}

the above code works perfectly, but I would like to use an array of selectors instead of having to write a condition for each selector (I might have to check a lot of selectors).

here's what I've been trying so far :

var elems = $(".sf-item-352, .sf-item-356", .sf-item-362");

elems.each(function() {

     if ($(this).hasClass("sf-option-active")) {

          do_something();

     } else {

          do_something_else();
     }

});

I obviously don't get the same results... can somebody help me with this ? any advice ?

thanks

Aucun commentaire:

Enregistrer un commentaire