The browser passes an event object as the first argument to every listener. Name it event (or e) and read its properties.
event.type is the event name, event.target is the element where the event started, and event.key tells you which key was pressed.
event.shiftKey, event.ctrlKey, event.altKey and event.metaKey are true while those keys are held, so you can build shortcuts such as Ctrl+S.
event.preventDefault() cancels the browser's normal action: following a link, submitting a form, or typing a character.
event.target is the innermost element that was clicked; event.currentTarget is the element whose listener is running. They differ when you click a child inside the listening element.
currentTarget is only set while the event is being dispatched. Logging the event later shows currentTarget: null, which surprises people reading it in DevTools.
defaultPrevented reports whether some listener cancelled the default action; cancelable says whether it can be cancelled at all (for example, scroll events cannot).
Use event.key for the character or key name ("a", "Enter", "ArrowUp") and event.code for the physical key ("KeyA"), which matters for games on different keyboard layouts.
05Definitions
Event object
The object passed to a listener describing the event: its type, target and details.
event.target
The element where the event started, such as the exact element that was clicked.
event.currentTarget
The element whose listener is currently running.
preventDefault
A method that cancels the browser's default action for the event.
Modifier key
Shift, Ctrl, Alt or Meta, reported by shiftKey, ctrlKey, altKey and metaKey.
Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.
Which button was clicked?
Use currentTarget instead of a shared variable.
const buttons = document.querySelectorAll(".size");
let chosen = "";
for (const button of buttons) {
button.addEventListener("click", () => {
chosen = buttons[0].dataset.size;
});
}
buttons[2].click();
console.log("Size:", chosen);
Move with the arrow keys
Branch on event.key.
let x = 0;
document.addEventListener("keydown", (event) => {
// TODO: ArrowRight adds 1, ArrowLeft subtracts 1, other keys do nothing
document.querySelector("#pos").textContent = x;
});
for (const key of ["ArrowRight", "ArrowRight", "a", "ArrowLeft", "ArrowRight"]) {
document.dispatchEvent(new KeyboardEvent("keydown", { key }));
}
console.log("x =", x);
Confirm external links
Cancel a default action conditionally.
// Write guard(link): it adds a click listener that cancels the click
// (preventDefault) only for links with a data-external attribute,
// and logs "Blocked: <href>" when it does.
09Prediction
Commit to an answer first, then run the real code to compare.
A code editor in the browser cancels Tab's default focus change and inserts spaces instead, and uses Ctrl or Cmd plus S to save without opening the browser's own dialog. Both read the event object and call preventDefault only for those keys.
2. A listener on a ul logs event.currentTarget.tagName. An li inside it is clicked. What is printed?
LI
UL
BODY
undefined
3. What does event.preventDefault() do?
Stops other listeners
Cancels the browser's default action
Removes the listener
Stops the event from bubbling
4. Why does this shortcut never fire for Ctrl+S?
if (e.ctrlKey && e.key === "S") save();
The key name is "s", not "S", when Shift is not held
ctrlKey does not exist
keydown has no key
The handler must return true
5. Which property tells you whether Shift is held?
event.shift
event.shiftKey
event.modifiers
event.key === "Shift" always
6. Three buttons share one handler. Which way of reading the clicked button's data-id is most reliable?
A loop variable captured with var
event.currentTarget.dataset.id
document.querySelector("button").dataset.id
this inside an arrow function
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Which button was clicked?: Prints Size: L; Medium works too
Move with the arrow keys: Prints x = 2; The page shows 2
Confirm external links: Prints the blocked link; Internal links are not cancelled
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-12-events-and-interaction/lesson-057-event-objects/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
Listeners receive an event object.
target is where the event started; currentTarget is where the listener is.
key and the modifier flags describe keyboard input.
preventDefault cancels the browser's default action.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationevent objects
Context: I am learning event objects in JavaScript.
Task: Explain event.target, event.currentTarget, event.key and preventDefault with small HTML examples.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionevent objects drill
Context: I have practised event objects.
Task: Give me six event-object puzzles about target and currentTarget and ask what each prints.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingevent objects bug
Context: My shared click handler always reads the first button instead of the one that was clicked.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationevent objects practice
Context: I finished the event objects exercises.
Task: Create three exercises that use event.key, modifier keys and preventDefault.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewevent objects review
Context: Here is my code that uses event objects.
Task: Review my event handlers for confusion between target and currentTarget.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.