Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#13949 closed feature (plugin)

Multiple arguments support in hasClass

Reported by: [email protected] Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.9.1
Keywords: Cc:
Blocked by: Blocking:


Sometimes, we need to check multiple class with hasClass,

if ( $el.hassClass("class1") && $el.hasClass("class2") && ...) {
  //Yes It does

It would be great if supports multiple classNames like

if ( $el.hasClass("class1, class2, class3") ) {
  //Yes It does

Change History (5)

comment:1 Changed 9 years ago by anonymous

Actually it does support multiple classes just yet:

if ($el.hasClass("class1 class2 class3")) { Yes It does }

comment:2 Changed 9 years ago by m_gol

#13948 is a duplicate of this ticket.

comment:3 Changed 9 years ago by m_gol

hasClass doesn't support multiple classes, its behavior on input with multiple classes is unspecified:

$('<div>').addClass('a b').hasClass('a b')
$('<div>').addClass('a b').hasClass('b a')

I'm against such a change, it's not obvious if hasClass('class1 class2') is supposed to mean that element has all of those classes set or at least one of them. If you really need it, you can define a very small plugin (warning: not tested):

jQuery.fn.hasAllClasses = function( classesString ) {
    var i,
        classes = ( classesString || '' ).match( /\S+/g ) || [];
    for ( i = 0; i < classes.length; i++ ) {
        if ( !this.hasClass( classes[ i ] ) ) {
            return false;
    return true;
Last edited 9 years ago by m_gol (previous) (diff)

comment:4 Changed 9 years ago by dmethvin

Resolution: plugin
Status: newclosed

This does seem like perfect plugin material to me. You can implement hasAllClasses, hasAnyClass, or whatever. You could also do it using $el.filter(".class1.class2").length > 0 or similar.

comment:5 Changed 9 years ago by [email protected]

I agree that .hasClass("a b").hasClass("b a") must be true.

Note: See TracTickets for help on using tickets.