Javascript DOM MouseEvent which Property

Introduction

Find out which mouse button that was pressed when a mouse event was triggered:

View in separate window

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>


<div onmousedown="WhichButton(event)">Click this text with one of your mouse buttons to return a number.
</div>//from  w ww. jav a  2s . c  om

<script>
function WhichButton(event) {
  document.getElementById("demo").innerHTML = "You pressed button: " + event.which;
}
</script>

</body>
</html>

The which property returns a number telling which mouse button was pressed when a mouse event was triggered.

The values returned by this property are the same as those returned by the button property plus one.

This property is read-only.

Possible values:

  • 0 : No button
  • 1 : Left mouse button
  • 2 : Wheel button or middle button (if present)
  • 3 : Right mouse button



PreviousNext

Related