Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  176] [ 5]  / answers: 1 / hits: 25428  / 10 Years ago, fri, december 19, 2014, 12:00:00

I needed to use element.classList.contains('some-class') to check if an element contains at least one of three classes, so I wrote:



if (element.classList.contains('class1', 'class2', 'class3')) {
// do stuff here
}


The result was - if the element has 'class1', it returns true, no matter if it has 'class2' or/and 'class3', and
// do stuff here
executes. However, if it has 'class2' and/or 'class3', it returns false and
// do stuff here
does not execute.



I worked around it with:



if (element.classList.contains('class1') || element.classList.contains('class2') || element.classList.contains('class3')) {
// do stuff here
}

More From » javascript

 Answers
23

This function only takes one parameter. The contains method is used to check if your classList contains a singular element. The code block that you have posted at the bottom is a good workaround. But unfortunately what you are trying to do at the top is beyond the reach of the classList API.


[#68436] Wednesday, December 17, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarrettw

Total Points: 300
Total Questions: 98
Total Answers: 103

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;