Skip to content

Instantly share code, notes, and snippets.

@jeangontijo
Created September 2, 2020 23:12
Show Gist options
  • Save jeangontijo/44ea355d91bcfa4cf9edcc18812de2c3 to your computer and use it in GitHub Desktop.
Save jeangontijo/44ea355d91bcfa4cf9edcc18812de2c3 to your computer and use it in GitHub Desktop.
Toggle Class on Click
function toggleClass(element, oldClass, newClass) {
let e = document.querySelector(element).classList;
if(e.contains(oldClass)){
e.replace(oldClass, newClass);
}else{
e.replace(newClass, oldClass);
}
}
button.addEventListener('click', () => {
toggleClass('.element', 'oldClass', 'newClass');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment