Learning Objectives
- Read the core properties:
type,target,currentTarget,timeStamp - Use pointer data (
clientX/clientY,button) and key data (key,code, modifier flags) - Cancel an event's default action with
preventDefault() - Build a start/stop event watcher from paired
addEventListener/removeEventListenercalls
One object, many shapes
Every event carries the basics — type, target, timeStamp — and then a layer of properties specific to its kind. A keydown has key and code; a mouse event has coordinates and a button number; both carry the modifier flags altKey, ctrlKey, shiftKey. The demo below dumps one fixed list of properties for every event it sees:
Ask a keyboard event for clientX and you get undefined — not every property is meaningful for every event, and the watcher makes that visible rather than hiding it. For open-ended exploration, console.dir(e) inside any handler shows you everything an event actually carries.
Live demo
Press Start watching, then generate events: type in the textarea (try holding Alt, Shift, or Ctrl), move the mouse through the tracking area, click the button. Each event overwrites the property log on the left. Stop watching detaches every listener — the page goes quiet, which is its own lesson.
The start/stop pair works because the handlers are kept in one object, so the same function references get attached and detached:
Canceling the default
Many events announce something the browser is about to do: a click on a link navigates, a submit posts the form, a keydown types a character. preventDefault() is your veto. The handler still runs, the event still travels the tree — only the built-in consequence is canceled. You saw it in step 1 on a link; it returns in step 5 as the heart of form handling, where canceling the submit is what makes client-side processing possible.
Next Steps
In Step 3: Event flow, we follow one event's whole journey through the tree:
- Trace the three phases: capture, target, bubble
- Read
eventPhaseto see where a listener sits in the journey - Stop the journey with
stopPropagation()— and see what it does not stop
