Marcin Bobowski
Marcin Bobowski

Reputation: 1775

jQuery - check if element has all classes from an arrray

I have a dinamically build array of classes:

var classes = new Array();
classes.push('class1');
classes.push('class2');
classes.push('class5');

and would like to select with jquery any element, that has all of those classes (but not only those classes), eg:

<a class="class1 class2 class5 class10 class18">my element</a>

How should I slove that?

Upvotes: 2

Views: 1485

Answers (2)

Darshan
Darshan

Reputation: 535

You can try this color is your array of class names here is piece of code

  for ( var i = 0; i < color .length; i++ )
    {
      if ( $(this).hasClass( color[i] ) )
      {       
        break;  
      }
    }

also check out this http://api.jquery.com/jQuery.inArray/

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382394

You may get your elements with

$('.'+classes.join('.'))

In your case, the resulting selector would be ".class1.class2.class5".

Note that this assumes there is at least one element in your classes array.

Upvotes: 7

Related Questions