bragboy bragboy - 2 months ago 12
HTML Question

How to get a div with multiple classes count using jQuery

I have a sample HTML like the following,

<div class="class1"></div>
<div class="class2 class3"></div>


And my JS is,

alert(jQuery(".class1").length);
alert(jQuery(".class2 class3").length);


For the first alert I am getting 1 as expected, but I am getting 0 for the second alert. How to get the value 1 for the second alert as well. I need specifically this case (where a div has both class2 and class3). Help me.

Thanks

Answer

You want '.class2.class3'

What you have ('.class2 class3') means "A <class3> element that is a descendent of an element that is a member of class2".