What a difference a space makes

27 March, 2024

Sometimes you need your selector to look for two classes...but what do you do when both classes are for the same element?

In normal circumstances, you might be looking to select class=red and class=blue

<div class="red">
  <div class="blue">
  </div>
</div>

and would use the selector

$('.red .blue')

However, if you need to select the element that has both red and blue as its classes

<div class="red blue">
</div>

then the selector looks the same, but without a space between the classes

$('.red.blue')

This won't work if one of the classes is represented by a variable. In that case, do this:

$('.red.'+var)

 

Login or Register to Comment!