Events: Making Pages Respond

A page that answers back: run code when someone clicks, types or presses a key, read what happened from the event object, handle a whole list with one listener, make every control work from the keyboard, and draw the page from your data.

Updated

Events in JavaScript

  1. Responding to Clicks with addEventListener (Apprentice): An event is something that happens on the page, such as a click, and addEventListener tells the browser which of your functions to run each time it happens.
  2. The Event Object (Apprentice): Every listener is handed an event object that says what just happened, so one function can tell which button was clicked, which element it landed on, and which key was pressed.
  3. Bubbling and Event Delegation (Journeyman): A click travels up from the element it happened on through every parent, so one listener on a list can handle every button inside it, even buttons added later.
  4. Keyboard Users and Accessible Controls (Journeyman): Build controls that work for people who use a keyboard or a screen reader, with real buttons, aria-pressed and aria-expanded that match what the page shows, and an Escape key that closes what's open and puts focus back.

Drawing the Page from Data

  1. Drawing the Page from Your Data (Journeyman): Keep what the page shows in an array, write one render function that draws the page from it, and have every listener change the data and call render, so the page and the data never disagree.

Exercise: Recipe Filters

  1. Exercise: Recipe Filters (Journeyman): Bring the Weeknight Recipes page to life, drawing its cards from data, filtering them with buttons, opening each card's steps with one delegated listener and closing them with Escape, and let the tests and a checklist confirm it works.