Handling Events
Prerequisites: Working with the DOM
An event is something that happens in the browser — a click, a key
press, a form submission. addEventListener runs a function whenever
a given event occurs on an element.
const button = document.querySelector("#save-button");
button.addEventListener("click", () => {
console.log("Button was clicked!");
});
Common mistakes
- Forgetting that the callback function isn't run immediately — it's only registered here, and runs later, whenever the event actually happens.