Track 02 · Lesson 2 of 17Inside the browser

Track 02 · Inside the browser

What fires when you click or type

Lesson 2 of 17· 7 min

One click is not one event. The browser fires a whole sequence, and every event carries an object full of details. Use the controls and watch the log; click any log row to inspect its event object.

Record everything

Click, type, tick, pick

Everything in this box is being listened to

Event log, oldest first (click a row)

Interact with the controls on the left.

The selected event object

Click a row in the log.

For one mouse click: pointerdown → mousedown → focus → pointerup → mouseup → click. For one keystroke in a text field: keydown → beforeinput → input → keyup. Touch screens fire pointer events too.

Pick the event that matches your intent#

The browser fires low-level events (pointerdown, keydown) and higher-level ones (click, input, submit). Reach for the high-level one whenever it fits:

  • click for activating something. It also fires when a keyboard user presses Enter or Space on a button, so it keeps your page usable without a mouse.
  • input for reacting to typing as it happens.
  • submit for forms, rather than a click on the submit button. Pressing Enter in a field submits the form too, and only submit catches both.
  • Pointer events (pointerdown, pointermove) when you need the low-level detail. They cover mouse, pen and touch with one listener.

What's inside an event object#

Every event has type, target, currentTarget, timeStamp and isTrusted. Keyboard events add key and code; pointer events add coordinates and pointerType. isTrusted is false for events your own code dispatches, which is how a page can tell a real click from a fake one.

Check yourself

You click a button with a mouse. Which of these events fires last?