The browser
The event object: event.target, currentTarget, key
A note from the browser: what happened, where, and the details.
Why it exists
Knowing "there was a click" isn't enough. In a list of a hundred products you need to know which one was clicked. For shortcuts, which key was pressed and whether Ctrl was held. In an input field, what it says now. Instead of a handler per button and per key, the browser passes a single object with all the details, and one handler can sort things out by itself.
Where it's used
- Delegation:
event.target.closest(".add")tells you which button in a list was clicked. - Keyboard shortcuts:
event.key === "Escape",event.ctrlKey && event.key === "s". - Input fields:
event.target.valueis the current text in a search box. - Cancelling browser actions: event.preventDefault() on links and forms.
Syntax
element.addEventListener("click", (event) => {
event.type; // "click"
event.target; // where it happened
event.currentTarget; // where the handler lives
});
field.addEventListener("keydown", (event) => {
event.key; // "Enter", "Escape", "a"…
});Examples
const button = new EventTarget();
let seen;
button.addEventListener("click", (event) => {
seen = [event.type, event.target === button, event.currentTarget === button];
});
button.dispatchEvent(new Event("click"));
seen; // → ["click", true, true]const button = new EventTarget();
let saved;
button.addEventListener("click", (event) => {
saved = event;
});
button.dispatchEvent(new Event("click"));
saved.target === button; // → true
saved.currentTarget; // → null — the trip is over, the browser cleared ittarget stays, currentTarget only lives while handlers runfunction shortcut(event) {
if (event.target.tagName === "INPUT") return null; // someone is typing
if (event.key === "Escape") return "close";
if (event.ctrlKey && event.key.toLowerCase() === "s") return "save";
return null;
}
shortcut({ key: "Escape", target: { tagName: "BODY" } }); // → "close"
shortcut({ key: "S", ctrlKey: true, target: { tagName: "BODY" } }); // → "save"
shortcut({ key: "Escape", target: { tagName: "INPUT" } }); // → nullResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
An event travels through the tree: from window down to the element where it happened, and back up again (bubbling). There is one event object for the whole trip, and the browser hands it to every handler along the way. So:
event.targetstays the same the whole time: the deepest element where the event started (it may be a<span>inside a button);event.currentTargetchanges: it's the element whose handler is running right now. Once the event has finished its trip, the browser setscurrentTargetto null.
Different events carry different details: keyboard events have key (the character, respecting layout and case) and code (the physical key, "KeyL"), mouse events have coordinates and button, and every event has type, timeStamp, defaultPrevented. In a function handler this equals currentTarget, but arrows have no this of their own, so event.currentTarget is the safer choice.
Traps
When to pick something else
Don't keep the event object "for later" or pass it deep into your program: pull out what you need (key, target.value, dataset) right away and work with plain values. That keeps the code easy to test: a function of a string can be tested without a page.