Module 19: Browser Interactions

Lesson 3 of 5 · 15–25 minutes

Events

🏠The everyday picture

A doorbell. Nothing happens until someone presses it; then it rings. You decide in advance what happens when it's pressed.

📘What it really is

const button = document.querySelector("#menu-button");
button.addEventListener("click", () => {
  document.querySelector("nav").classList.toggle("open");
});

🖱️Try it

Build a menu button for phones:

  1. Add <button id="menu-button" type="button" aria-expanded="false">Menu</button> to your header.
  2. In CSS, hide the nav on small screens unless it has the class open, and hide the button on large screens.
  3. In main.js, toggle the class open on the nav when the button is clicked, and set aria-expanded to "true" or "false" to match (screen readers use it to announce whether the menu is open).

✅Quick check

Which event fires every time someone types a letter in a field?

Show the answer

input.

Why set aria-expanded on a menu button?

Show the answer

It tells screen-reader users whether the menu is open or closed.

← PreviousSelecting and changing elements
ProLearners Assist