MouseEvent buttons Property - Javascript DOM

Javascript examples for DOM:Mouse Event

Description

The buttons property indicates which mouse buttons were pressed.

This property is read-only.

Return Value

A Number, representing one or more mouse buttons that were pressed.

For example, if the left button (1) and the right button (2) are pressed, the returned value is 1+2, which is 3).

Possible values:

  • 1 : Left mouse button
  • 2 : Right mouse button
  • 4 : Wheel button or middle button
  • 8 : Fourth mouse button
  • 16 : Fifth mouse button

The following code shows how to find out which mouse button(s) that was pressed:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<div onmousedown="WhichButton(event)">Click this text</div>

<h2>You pressed button: <span id="demo"></span></h2>

<script>
function WhichButton(event) {/*from  w w  w  . j  ava2 s  .com*/
    var x = event.buttons;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials