fritz carlton
fritz carlton

Reputation: 71

How to select an element that must have more than one css class?

I have an element that has two classes at all times:

<p class="general_description laptop">
<p class="images laptop">
<p class="specs laptop">

One class describes the item (laptop) and the other (general_description, images, specs) describes the state that the page needs to be in to present one of three types of information about the item.

How can I show or hide the <p> element only if both classes in my selector are present?

For example, if I only want to show the <p> element that corresponds to both the specs and laptop classes, I have tried doing this but it does not work:

$(".specs .laptop").show();

Upvotes: 2

Views: 340

Answers (3)

Joe Chung
Joe Chung

Reputation: 12123

Get rid of the space between the class names.

$(".specs.laptop").show();

Reference: jQuery

Upvotes: 1

gahooa
gahooa

Reputation: 137252

IIRC, it is:

$(".specs").filter(".laptop").show()

Upvotes: 1

Darko
Darko

Reputation: 38860

$(".specs.laptop").show();

OR

you could use the is() function in jquery. it takes a selector and tells you if the element conforms to it:

$(".specs").is(".laptop"); //returns true if the element has the laptop class

Upvotes: 4

Related Questions