Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting Elements with more than one Class in jQuery

Tags:

jquery

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).

like image 289
Josh Stodola Avatar asked Aug 26 '09 20:08

Josh Stodola


People also ask

How can I select an element with multiple classes in jQuery?

The . class selector can also be used to select multiple classes. Note: Seperate each class with a comma. Note: Do not start a class attribute with a number.

How can I select an element with multiple classes in Javascript?

Use the getElementsByClassName method to get elements by multiple class names, e.g. document. getElementsByClassName('box green') . The method returns an array-like object containing all the elements that have all of the given class names.

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.


1 Answers

$('.error.message')

should do it.

like image 146
JorenB Avatar answered Sep 30 '22 10:09

JorenB