on-internet
← Community

Javascript how to add active class to clicked element and hide all others

Stack Overflow ·

I have a list-item-menu, where when I click on one of them it gets an active class, while I want all other list-items to get hidden but I'm not sure how to do that. Here is my script so far: const items = document.querySelectorAll('.menu-item'); items.forEach((item) => { item.addEventListener('click', (e) => { item.classList.toggle('active') // HIDE ALL OTHER "item"-elements }) }); can someone help me out?

Comments

Sign in or sign up to comment and upvote.

No comments yet.