Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท DOM: Events

Click and pointer events

Reference for click, dblclick, contextmenu, mousedown, mouseup, mouseover, mouseenter and pointer events, with coordinates, buttons, modifier keys and examples.

Mouse input produces a family of events. The most used is click, which fires after a full press and release on the same element, and which also fires for keyboard activation (Enter or Space on a button) and for taps. Around it sit mousedown, mouseup, dblclick, contextmenu, and the hover events mouseover, mouseout, mouseenter, mouseleave and mousemove.

Pointer events (pointerdown, pointermove, pointerup, pointercancel, pointerenter, pointerleave and others) are the modern superset. One set of events covers mouse, pen and touch, and event.pointerType tells you which one it was.

JavaScript
button.addEventListener("click", (event) => {
  console.log(event.clientX, event.clientY, event.shiftKey);
});

Try it

Position, button and modifier keys
Dragging with pointer events

Syntax

JavaScript
element.addEventListener("click", (event) => { /* MouseEvent / PointerEvent */ });
element.addEventListener("pointerdown", (event) => { /* PointerEvent */ });

Common event properties

Property Description
clientX, clientY Position relative to the viewport.
pageX, pageY Position relative to the whole document, including scroll.
offsetX, offsetY Position relative to the target element's padding edge.
screenX, screenY Position on the screen.
button Which button changed: 0 main, 1 middle, 2 secondary, 3 back, 4 forward.
buttons Bit mask of buttons currently held: 1 main, 2 secondary, 4 middle.
shiftKey, ctrlKey, altKey, metaKey Whether the modifier was held.
detail For click/dblclick, the number of consecutive clicks.
pointerId, pointerType, isPrimary Pointer events only: identifies the pointer ("mouse", "pen", "touch").
pressure, width, height Pointer events only: contact details where the hardware reports them.

Usage notes

  • Order of events. A normal mouse click gives pointerdown, mousedown, pointerup, mouseup, click. Two quick clicks end with dblclick.
  • Use click for actions. It also fires for keyboard and assistive technology activation of buttons and links, so it is the accessible choice. Do not put click handlers on non-interactive elements such as <div> without a role and keyboard support; use a <button>.
  • mouseenter vs mouseover. mouseenter and mouseleave do not bubble and do not fire again when moving between children. mouseover and mouseout bubble and do fire for each child, which complicates hover logic.
  • Right and middle clicks. click fires only for the main button. Use auxclick for the others and contextmenu for the menu.
  • Touch. Set touch-action: none on elements you drag with pointer events, otherwise the browser may claim the gesture for scrolling and send pointercancel.
  • Pointer capture. setPointerCapture() keeps pointermove and pointerup coming to your element even when the pointer leaves it.
  • Hover is not available on touch. Never rely on hover alone for essential interactions.

Browser support

Mouse events work everywhere. Pointer events are supported by all current browsers.