#13949 closed feature (plugin)
Multiple arguments support in hasClass
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | 1.9.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
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
comment:3 Changed 9 years ago by
hasClass
doesn't support multiple classes, its behavior on input with multiple classes is unspecified:
$('<div>').addClass('a b').hasClass('a b') true $('<div>').addClass('a b').hasClass('b a') false
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; };
comment:4 Changed 9 years ago by
Resolution: | → plugin |
---|---|
Status: | new → closed |
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.
Actually it does support multiple classes just yet:
if ($el.hasClass("class1 class2 class3")) { Yes It does }