Created
September 2, 2020 23:12
-
-
Save jeangontijo/44ea355d91bcfa4cf9edcc18812de2c3 to your computer and use it in GitHub Desktop.
Toggle Class on Click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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