I'm trying to figure out how to write an if statement that works something like this:
if (element contains class 1 && class 2) {
// do this
}
How do I write that if statement to check for multiple classes?
Is there a way to make the .contains method check for more than one class?
What I've found so far:
How to check class of multiple class elements in javascript, without jquery
Problem is, it seems to be returning an array of all the classes that the element contains, which is not what I want. I need the function to check if the element contains the classes that I am supplying it with.
One of the solutions looks to me like it's asking us to create our own function to do this, but is there a native method on JS which will do the trick?
I'm quite new to this stuff and I really appreciate the patience you guys have shown in answering my questions.
I'm trying to figure out how to write an if statement that works something like this:
if (element contains class 1 && class 2) {
// do this
}
How do I write that if statement to check for multiple classes?
Is there a way to make the .contains method check for more than one class?
What I've found so far:
How to check class of multiple class elements in javascript, without jquery
Problem is, it seems to be returning an array of all the classes that the element contains, which is not what I want. I need the function to check if the element contains the classes that I am supplying it with.
One of the solutions looks to me like it's asking us to create our own function to do this, but is there a native method on JS which will do the trick?
I'm quite new to this stuff and I really appreciate the patience you guys have shown in answering my questions.
Share Improve this question asked Sep 13, 2018 at 22:57 EnterPasswordEnterPassword 5981 gold badge7 silver badges25 bronze badges 2 |3 Answers
Reset to default 17You can use the .matches()
API on the element, and it's pretty well-supported now:
if (element.matches(".class1.class2")) ...
It's like a built-in version of jQuery .is()
.
Documentation link.
Put the class names you want to test for into an array, then use Array.prototype.every()
to check if all members of your array exist in the element's classList
:
console.log(['a', 'b'].every(e=>div.classList.contains(e)))
console.log(['a', 'b', 'c'].every(e=>div.classList.contains(e)))
<div class="a b" id="div"></div>
Unfortunately Element.classList
is read-only, so you cannot add anything to its prototype. You can however do that on the Array.prototype
:
Array.prototype.contains = function(...args) {
return [...args].every(c=>this.includes(c))
}
console.log([...div.classList].contains('a', 'b'))
console.log([...div.classList].contains('a', 'c'))
console.log([...div.classList].contains('a', 'c.d'))
<div class="a b c.d" id="div"></div>
Important: Please note that this violates the basic OOP rule Do not modify objects you don't own. As such, it would be better to create a named function taking the element and an array with a list of classes to test for.
There is a native API for the browser that lets you play with the DOM. jQuery is not always needed. https://www.w3schools.com/jsref/prop_element_classlist.asp.
You can use the 'classList.contains' method
document.getElementById({id}).classList.contains({class}) -- returns true or flase
matches
is the answer, if element.classList returns all the classes of an element, how hard is it to compare it to a list of classes you are supplying? it's programming 101. – mpm Commented Sep 13, 2018 at 23:01