TIL how to check if a dom element has a specific class
POSTED ON:
Trying to figure out if your button has a .red
or .blue
class in JS?
Destructure the classList into an array so you can use include().
const theButtonsClasses = [ ...document.querySelector("#theButton").classList];
if (theButtonsClasses.includes("red")) {
console.log("This button has the red class!");
} else if (theButtonsClasses.includes("blue")) {
console.log("This button has the blue class!");
}
MDN On Includes:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
Related TILs
Tagged: js