JoyceLee JoyceLee - 1 year ago 78
jQuery Question

Select elements with multiple classes in jQuery

If an element has multiple classes like this:

class="btn btn-primary add-movie-button is-on"

can I select it only by one class name using jQuery like:


Can I use
for this element?

Answer Source

With a class on an element like so: class="btn btn-primary add-movie-button is-on"

You can select by one or more than one class. Adding more classes increases specificity.

For example:


is less specific than


In either case you can use the hasClass returning a boolean;

var isClassy = $("").hasClass("is-on");
var isSmallClassy = $(".add-movie-button").hasClass("is-on");

Note that this would return true if ANY element matched the conditions. If you desire to see if ALL do then use length:

var longClasses = $("");
var shortClasses = $(".add-movie-button");

var both = longClasses.length === shortClasses.length;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download