MouseEvent button Property - Javascript DOM

Javascript examples for DOM:Mouse Event

Description

The button property indicates which mouse button was pressed.

This property is read-only.

Return Value

A Number, representing which mouse button that was pressed.

Possible values:

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

The following code shows how to find out which mouse button 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) {/* www . j a v a 2  s .  c o  m*/
    var x = event.buttons;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials