Skip to content

Instantly share code, notes, and snippets.

@brayoh
Created January 13, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brayoh/d072d7fd0b0c1e843eb40499e705e366 to your computer and use it in GitHub Desktop.
Save brayoh/d072d7fd0b0c1e843eb40499e705e366 to your computer and use it in GitHub Desktop.
code snippet for changing an element class name(s)
addClass(el, clas, callback) {
if (el) {
const contains = el.classList.contains(clas);
if (contains) {
// dont add the class again
} else {
el.classList.add(clas);
}
if (typeof callback === 'function') {
callback.call();
}
}
}
removeClass(el, clas, callback) {
const contains = (el !== undefined) ? el.classList.contains(clas) : false;
if (contains) {
el.classList.remove(clas)
}
if (typeof callback === 'function') {
callback.call();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment