In the last video, you selected door-check’s booking reference search input by its class and narrowed it with instanceof, so the checker lets you read its .value. Its input handler receives an event too, and that event’s .target is still typed EventTarget | null. The search box gets that handler in question 1 at the end of this lesson, where you make it respond as the ushers type.
Rosa Delgado’s ushers need more than the search, though. They click a Check in button on a party’s line and type how many guests are at the door. In this lesson, you’ll read event.target with a type the checker can trust, and you’ll turn a number input’s value into a number. Start with the buttons, where one listener has to find out which button was clicked.
Handle Every Check-In Button With One Listener
On a full night, the door list has a Check in button on every line. One listener on the list can handle them all, because each click bubbles up from its button to the list. That’s event delegation, and Making the Page Respond covers it if you’d like a refresher.
The loop in src/main.ts that builds the list gives each button its booking’s reference:
src/main.ts
const button = document.createElement("button");
button.textContent = "Check in";
button.dataset.reference = booking.reference;
item.append(button);
The listener learns which button was clicked from event.target, and that’s where the type gets in the way. Every event has a .target. Its type, EventTarget | null, is declared once on Event for every listener on every element, so it can’t depend on where you added the listener. An event that your code creates and never sends anywhere has no target at all, which is where the null comes from.
EventTarget is the browser’s most general type for anything that can receive events, which includes the page’s elements, document, and window. Each DOM type below it adds to the one above it.