46 Votes

jQuery: Selector for all elements having two specific classes

Question by NetLabel | 2012-06-22 at 19:37

I am looking for a jQuery selector to select all elements that have assigned two specific classes. So for example:

<span class="a b"></span> [I only need this element]
<span class="a"></span>
<span class="b"></span>

In this example, I would like to select only the element, that has both classes, "a" as well as "b" (first line). Is that possible?

ReplyPositiveNegative
3Best Answer5 Votes

While you are using $(".a") for one class, you can simply connect the names of several classes with a dot in a row (without spaces) in order to select multiple classes:

$(".a.b").show();

In the same manner, you can also use this for elements with a specific ID:

$("#c.d.e").show();

This selects the element with the ID "c" and the two classes "d" and "e".

And for completeness:

$("div#c.d.e").show();

This selects a DIV with the ID "c" and the classes "d" and "e", so for example this one here:

<div id="c" class="d e"></div>

If you still need more than two classes, you can write them also to the row of classes connected with a dot.
2012-06-23 at 06:46

ReplyPositive Negative
Reply

Related Topics

jQuery: Show and hide elements

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.