Events

A page that never reacts is only a document. The moment you want something to happen when a person clicks a button, types in a search box, or submits a form, you need a way to say "when this happens, run this code". That is what events are. This chapter is about listening for the things a user does and responding to them.
Listening for an event
An event is something that happens on the page: a click, a key press, a form being sent. To respond to one, you pick an element, name the event you care about, and hand over a function to run when it happens.
That function is called a handler. The method that wires it up is addEventListener:
const button = document.querySelector(".buy-button");
button.addEventListener("click", () => {
console.log("Someone clicked buy");
});Read it left to right: on this button, listen for "click", and when it happens, run this function. The handler runs every time the event happens, not once. Click three times and it runs three times.
Handlers are ordinary functions, and the elements you attach them to come from the DOM. If you can select an element and write a function, you can respond to what the user does to it.
"click", and give addEventListener a function to run. That function is the handler, and it runs every single time the event happens, not once. This one method is behind almost everything a page does in response to you. The event object
When the browser runs your handler, it hands it one piece of information: an event object describing what happened. Catch it by giving your handler a parameter, usually named event:
const button = document.querySelector(".buy-button");
button.addEventListener("click", (event) => {
console.log(event.target); // the element that was clicked
});The most useful part is event.target: the element the event happened on. If the click was on the button, event.target is that button. You do not have to select it again, the event already tells you where it came from.
event to catch it. The part you will reach for most is event.target, the element the event happened on. No need to re-select it, the event already knows where it came from. Common events
A handful of events cover most of what you will build:
click: the user clicks or taps something.input: the value of a text field changes as they type.submit: a form is sent.keydown: a key is pressed.
The tricky one is submit. By default, submitting a form reloads the page, which wipes out anything your JavaScript was doing. To stop that, call event.preventDefault() inside the handler:
const form = document.querySelector("form");
form.addEventListener("submit", (event) => {
event.preventDefault(); // stop the page from reloading
console.log("Form submitted, page stays put");
});click, input, submit, and keydown. The one to remember is submit: a form reloads the page by default, so call event.preventDefault() in the handler to stop that and keep control. Miss it and your page refreshes out from under you, which is confusing the first time. Where events go from here
Events are how a page listens. You pick an element, name what you care about, and run a function when it happens, reading the details from the event object as you go. Almost every interactive feature is built from that loop.
The natural next step is doing something slow in a handler, like sending a form to a server or loading fresh data, without freezing the page. That is what async code is for, and it is the next chapter. Handlers that reach into structured data, like a list of items you keep in objects, lean on what you already know too.

