Toggle Classes
Toggle Classes
classList.toggle flips whether a class is present. It is useful for UI states that can move back and forth, such as open menus, selected filters, or expanded panels.
The key decision is where state belongs. Here the page state is represented by a class on the menu, so the click handler should change the DOM state instead of only changing a variable.
Your task: When the menu button is clicked, toggle the is-open class on the menu panel.
Free with a JS Exercises account
Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.
By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.
Toggle Classes
classList.toggle flips whether a class is present. It is useful for UI states that can move back and forth, such as open menus, selected filters, or expanded panels.
The key decision is where state belongs. Here the page state is represented by a class on the menu, so the click handler should change the DOM state instead of only changing a variable.
Your task: When the menu button is clicked, toggle the is-open class on the menu panel.