Skip to content

Instantly share code, notes, and snippets.

@yratof
Last active September 7, 2022 22:33
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 yratof/dfdd1662a4b84972bdf7eff2ba652aed to your computer and use it in GitHub Desktop.
Save yratof/dfdd1662a4b84972bdf7eff2ba652aed to your computer and use it in GitHub Desktop.
Toggle class with ES6 vanilla
// 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