How to check if an element has multiple classes with vanilla JS

How to check if an element has multiple classes with vanilla JS



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.






although 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
Sep 13 '18 at 23:01



matches






You're absolutely right of course, I was just looking to see if there was a quicker way to do it in future since I think I will be using this a good number of times :)

– Sanjay
Sep 13 '18 at 23:17




3 Answers
3



You can use the .matches() API on the element, and it's pretty well-supported now:


.matches()


if (element.matches(".class1.class2")) ...



It's like a built-in version of jQuery .is().


.is()



Documentation link.






That's exactly what I was looking for, thank you very much! :)

– Sanjay
Sep 13 '18 at 23:16






Good solution, even though I kind of dislike the need for a string builder here. How does your approach fare with class names containing a .?

– connexo
Sep 13 '18 at 23:27



.






@connexo I think the API allows them to be quoted with backslash like the jQuery one does, or if not you'd do it the same way as in a CSS file, by using some other sort of escape.

– Pointy
Sep 13 '18 at 23:31







No need for trickery like that with my approach. Just do [...element.classList].contains('a', 'c.d'), see my answer below. I know modifying built-in objects is risky and questionable, but elegant as well.

– connexo
Sep 13 '18 at 23:33



[...element.classList].contains('a', 'c.d')






@connexo there's always more than one way to do something like this in the front-end world :)

– Pointy
Sep 13 '18 at 23:35



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:


Array.prototype.every()


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:


Element.classList


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






Read again: Is there a way to make the .contains method check for more than one class?

– connexo
Sep 13 '18 at 23:07


.contains






my apologies... you can get an array of classes u want to check against and you iterate over it with 'contains' you can this looks like a good example below. stackoverflow.com/questions/6391448/…

– alee046
Sep 13 '18 at 23:11



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế