Course Recommendation:

MouseEvent which property

Definition and Usage

which property returns a number indicating which mouse button was pressed when the mouse event was triggered.

Note:This property is mainly used with the onmousedown event. The value returned by this property is equal to button property

Note:The return value is incremented by one.

This property is read-only.

Example

Find out which mouse button was pressed when the mouse event was triggered:

alert("You pressed button: " + event.which)

Try it yourself

Syntax

Technical Details

Return value:

Numeric values representing the mouse button pressed when the mouse event occurs.

Possible values:

  • 0: No button
  • 1: Left mouse button
  • 2: Scroll wheel button or middle button (if any)
  • 3: Right mouse button

Note:For left-handed mouse configuration, the return value is reversed.

DOM Version: DOM Level 2 Events

Browser Support

The numbers in the table indicate the first browser version that fully supports the property.

Properties Chrome IE Firefox Safari Opera
which 1.0 9.0 1.0 1.0 5.0

Related Pages

HTML DOM Reference Manual:MouseEvent button Property

HTML DOM Reference Manual:MouseEvent buttons Property