Last active
September 7, 2022 22:33
-
-
Save yratof/dfdd1662a4b84972bdf7eff2ba652aed to your computer and use it in GitHub Desktop.
Toggle class with ES6 vanilla
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
// The element you're targetting | |
// The class you're toggling | |
const el = document.querySelector('.menu-item-has-children'); | |
const handleToggle = () => el.classList.toggle('available'); | |
el.onclick = () => handleToggle(); | |
document.addEventListener('click', event => { | |
const isClickInside = el.contains(event.target) | |
if (!isClickInside) { | |
el.classList.remove('available') | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment