Skip to content

Instantly share code, notes, and snippets.

@yratof
Last active September 7, 2022 22:33
Embed
What would you like to do?
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