5

I have some message elements like this...

<span class="error message">Whoops!  Don't forget your name.</span>

And...

<span class="success message">All done!  Thank you very much.</span>

You'll notice that two classes are being applied to a single span element. This is valid markup. How can I select elements with two classes like this?

Note: I don't want all elements that have a class attribute containing "message". I need the elements that have both "error" and "message" (and nothing else).

1 Answer 1

14
$('.error.message')

should do it.

3
  • 3
    Note that this is also the correct syntax for a CSS definition for an element with multiple classes.
    – zombat
    Commented Aug 26, 2009 at 21:06
  • Take a look at all the possibilities in CSS 3 Selectors - they're awesome. When taking into account the fact that jQuery supports pretty much all of them, it means you have awesome power over your DOM.
    – JorenB
    Commented Aug 26, 2009 at 21:12
  • As does Prototype in version 1.6.1 (due out any day), as both jQuery and Prototype use the same underlying Sizzle selection engine from John Resig. Commented Aug 27, 2009 at 7:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.