TIL adding a helper function for CSS classes
POSTED ON:
TAGS: css javascript utility
I like this helper.
This lets you quickly add/remove CSS classes to your elements in a easy-to-read way.
function addClass(elems, ...classes) {
// Add the .color-blue class
for (let elem of elems) {
elem.classList.add(...classes);
}
}
function removeClass(elems, ...classes) {
for (let elem of elems) {
elem.classList.remove(...classes);
}
}
let p = document.querySelectorAll('p');
addClass(p, 'color-blue', 'text-large'); // add two classes to the p element
Related TILs
Tagged: css