Chapter practice task
Task: Build a mobile menu and an FAQ accordion
- Add a menu button that is only visible on small screens.
- Toggle an
is-openclass on the menu when the button is clicked. - Update
aria-expandedon the button so screen readers know the state. - Build an FAQ list where clicking a question opens its answer and closes the others.
- Close the menu when the visitor presses Escape.
By the end of this chapter
- You can select one or many elements with querySelector and querySelectorAll
- You can listen for clicks, input and other events with addEventListener
- You can change text, classes, attributes and styles from JavaScript
Quick answers for this chapter
What is the DOM?
The Document Object Model: the browser’s live, editable version of your HTML that JavaScript can read and change.
Should I change styles directly or toggle classes?
Toggle classes. Keep the styles in CSS and let JavaScript switch them on and off.
Is this how sliders and menus work?
Yes. Most interactive components are just events plus class changes.