Skip to the workspace
Pebellslearn by building
DOM APIs  ›  Lesson

DOM Events

DOM APIs 15 min

javascript / DOM Events

Explore each scene at your own pace.

01 · DOM Events

DOM Events

DOM Events

An event listener registers code to run when an event is dispatched. event.target identifies the originating target; currentTarget identifies the listener’s current element.

Register the response now; execute it when the interaction happens.

See the mechanism

Walk through the modelDOM EventsAuthored snapshots of the example; use Run below to test the code.
buttonclick listener
status"Not saved"
Registering instructions does not trigger the event.
1 of 3

Follow one example

html

The listener is attached to the button. Clicking its inner span may make target the span, while currentTarget remains the button during this handler. The status changes only when the event fires. Native buttons support keyboard activation; a click listener on a decorative div does not automatically provide equivalent semantics. A production save operation also needs persistence and failure handling.

Predict, change and check

Predict: If the inner span receives the click, which property should you use to disable the button holding the listener?

Test your explanation: Click the text, then activate the button with the keyboard after resetting the preview. Extend the mini-project: show a saved count and add a second article without using inline onclick attributes.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Follow the event through its route

Events can move through capture and bubbling phases. A listener on a parent can handle events from matching descendants, which is useful for a changing list. event.target identifies the origin, while currentTarget identifies the element whose listener is currently running. Not every event bubbles in the same way.

preventDefault cancels an event's cancelable default action, such as submitting a form, but does not mean 'stop bubbling'. stopPropagation affects propagation and does not automatically cancel a default action. Remove listeners with the matching callback and capture setting, or own them with a supported AbortSignal. Native controls still need accessible labels and visible focus.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.