logo

jQuery selecting elements with multiple classes

logo

The more complicated the CSS coding the more complicated selecting elements with jQuert can become.

You may find yourself with a situation such as:

<div class="cOne cTwo cThree"></div>

<div class="cOne cTwo"></div>

<div class="cOne cThree"></div>

So how do we select specific elements when all of these div’s contain the class “cOne”? The answer is actually very simple!

You concatenate the classes in your selector such as:

jQuery(‘div.cOne.cThree’)

So you may do something like this if you wanted to add a new style to only div’s that have both the class “cOne” and “cThree”:

if(jQuery("div.cOne.cThree").length != 0){

jQuery("div.cOne.cThree").addClass("superClass");
}

2 Responses to “jQuery selecting elements with multiple classes”

  1. Patrick says:

    Wow! That’s easy and simple, I can’t believe I never thought of that before! Thanks dude, you’re the man!

  2. Apple says:

    Thanks for the very detailed helpful tips on jQuery. I’m pretty new with this tough and I appreciate all the help I get, so THANKS big time! I owe you one!

Leave a Reply

logo
logo
Powered by Wordpress | Designed by Elegant Themes