Javascript DOM MouseEvent button Property

Introduction

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

View in separate window

<!DOCTYPE html>
<html>
<body>
<div onmousedown="WhichButton(event)">Click this text with one of your mouse buttons to return a number.
</div>/*w ww .ja v a 2s. c  o  m*/
<p id="demo"></p>
<script>
function WhichButton(event) {
  document.getElementById("demo").innerHTML = "You pressed button: " + event.button;
}
</script>
</body>
</html>

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

This property is mostly used together with the onmousedown event.

This property is read-only.

Possible values:

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



PreviousNext

Related