Today I Learned - Rocky Kev

TIL how to check if a dom element has a specific class

POSTED ON:

TAGS:

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:

TIL why you should always use textContent

Today I learned the difference between 'Node.textContent'. It handles all elements, even hidden ones. It also prevents XSS attacks since it strips tags.

TIL prepend

When you're creating new elements in Javascript, you want to attach the new element to something that exists in the DOM already. You do that with append, and prepend.

TIL the simulating a Pipe function

It’s a pipe function that allows you to chain multiple operations together by taking a series of functions as arguments and applying them in a specific order to the input.