Buttons, Movement, and Hover

Mouse events give you precise control over what happens when users interact with your page using a mouse or trackpad.

Primary Mouse Events

EventWhen it fires
mousedownA mouse button is pressed
mouseupA pressed mouse button is released
clickA full press-and-release on the same element
dblclickTwo clicks in quick succession
mousemoveThe cursor moves (fires for every pixel)
mouseoverThe cursor enters an element
mouseoutThe cursor leaves an element
contextmenuRight-click (before the context menu appears)

Which Mouse Button Was Pressed?

The event.button property tells you:

  • 0: Left button (primary)
  • 1: Middle button (scroll wheel click)
  • 2: Right button
Warning

dblclick fires AFTER two consecutive click events. If you need to handle both single and double clicks on the same element, you'll need to use a timer to differentiate them, or the double-click will always trigger the single-click handler too.

Tip

To prevent text selection when rapidly clicking, call event.preventDefault() on the mousedown event.