4 6 Votes

jQuery: Selector for all elements having two specific classes

Question by NetLabel | 22/06/2012 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

Stefan Trost

Show Profile | Message
Avatar
3Best Answer
5 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.
23/06/2012 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 articles on askingbox.com. How to do.