Skip to content

Instantly share code, notes, and snippets.

@Dinir
Created September 5, 2016 10:50
Show Gist options
  • Save Dinir/bbe03d0ea5c7ed3ee0bdc06d9252e5e7 to your computer and use it in GitHub Desktop.
Save Dinir/bbe03d0ea5c7ed3ee0bdc06d9252e5e7 to your computer and use it in GitHub Desktop.
Change DOM classes quickly.
const changeClass = (target, firstCl, secondCl) => {
// if first exist = add it
// if both exist = change first to second
// if both exist and first is * = overwrite with second
if(firstCl) {
if(!secondCl) {
target.className += ` ${firstCl}`;
} else {
target.className = firstCl==="*"?
secondCl:
target.className.replace(new RegExp('\\s?'+firstCl), secondCl);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment