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.
button.addEventListener("click", (event) => { console.log(event.clientX, event.clientY, event.shiftKey); });
Try it
Syntax
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 withdblclick. - Use
clickfor 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>. mouseentervsmouseover.mouseenterandmouseleavedo not bubble and do not fire again when moving between children.mouseoverandmouseoutbubble and do fire for each child, which complicates hover logic.- Right and middle clicks.
clickfires only for the main button. Useauxclickfor the others andcontextmenufor the menu. - Touch. Set
touch-action: noneon elements you drag with pointer events, otherwise the browser may claim the gesture for scrolling and sendpointercancel. - Pointer capture.
setPointerCapture()keepspointermoveandpointerupcoming 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.
Related
- keyboard events โ the keyboard counterpart
Event.preventDefault()Event.targetandcurrentTargetElement.getBoundingClientRect()- Lesson: Events