0

I'm trying to make a tab filter by jquery. I want to ask how to select only div has class type1 also has class type2

  <div class="type1 type2"> </div> //select this one
  <div class="type1 type4"> </div>

and check the selector length if they found

   var typeselect = $('select the div above');
   if(typeselect.length > 0 ){} found it
   else 
pexichdu
  • 849
  • 1
  • 10
  • 15

2 Answers2

1

You can use class selector like below,

var typeselect = $('div.type1.type2');
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

You use .type1.type2 (note: no space) to select only elements that have both classes. See the specification for more about selectors.

This is just an application of the general principal that selector elements can be combined. That is, you can combine an ID seletor like #foo with a class selector like .bar to be #foo.bar (only find the element if it has both the ID and the class). Or combining a class selector with an attribute selector: .foo[data-special] (only find the element if it has the class and that attribute). Or combining a tag selector with a pseudoclass: div:hover.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875