-3
<div id="test" class="a1 a2 a5"></div>

var element = document.getElementById("test") 
if (hasAnyOfTheseClasses(element, ["a1", "a6"])) { 
   //...
}

Looking for a simple, lightweight function to check if a function has any of the listed classes without jQuery or another library.

Such function would be easy to implement, but there should be a canonical, fastest and simplest answer people can just copy-paste.

This seems vampire-ish, but I'm asking this so googlers won't have to write it themselves.

Not a duplicate - the linked question checks for one class, this question asks for checking any of the classes.

A jQuery version exists here.

Community
  • 1
  • 1
noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67

6 Answers6

1

Here's a functional implementation using Array.some and Element.classList.contains.

function hasAnyClass(element, classes) {
  return classes.some(function(c) {
    return element.classList.contains(c);
  });
}

var div = document.getElementById("test");
console.log(hasAnyClass(div, ["hi", "xyz"]));
console.log(hasAnyClass(div, ["xyz", "there"]));
console.log(hasAnyClass(div, ["xyz", "xyz"]));
<div id="test" class="hi there"></div>

Note that these functions are not supported on older versions of IE, and will require a shim/polyfill.

4castle
  • 32,613
  • 11
  • 69
  • 106
0

Here's my implementation:

function hasAnyOfTheseClasses(element, classes) {
    for (var i = 0; i < classes.length; i++) {
        if ((' ' + element.className + ' ').indexOf(' ' + classes[i] + ' ') > -1) {
            return true;
        }
    }
    return false;
}

It's not elegant or fast. Works though. Feel free to edit to improve.

noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
0

You could use a regex, not sure that it's purely better but at least more flexible since your current test relies too much on spaces being entered correctly.

function hasAnyOfTheseClasses(element, classes) {
    var className = element.className;
    for (var i = 0; i < classes.length; i++) {
        var exp = new RegExp('\b'+classes[i] + '\b');
      if(exp.test(className)) return true;
    }
    return false;
}
Paul
  • 35,689
  • 11
  • 93
  • 122
  • *"relies too much on spaces being entered correctly"* I don't think so. How could that be an issue? That's also the implementation that jQuery uses btw. – Felix Kling Sep 27 '16 at 01:19
0

just create a loop that check if each value in your array is a class in your passed element

function hasAnyOfTheseClasses(elem, tofind) {
    classes = elem.className.split(' ');
    for(var x in tofind) {
        var className = tofind[x];
        if (classes.indexOf(className) == -1){
            return false;
        }
    }
    return true;
}
Beginner
  • 4,118
  • 3
  • 17
  • 26
0

Use the .classList property to get the list of classes of an element. Then you can use the .contains() method to test each of the classes.

function hasAnyOfTheseClass(element, classes) {
    var classList = element.classList;
    return classes.some(function(class) {
        return classList.contains(class);
    });
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

How about using Array.prototype.some() and Array.prototype.indexOf():

function hasAnyClass(el, classes) {
  var elClasses = el.className.split(' ');
  return classes.some(c => elClasses.indexOf(c) >= 0)
}
nbarbosa
  • 1,652
  • 1
  • 12
  • 10