MouseEvent which Property - Javascript DOM

Javascript examples for DOM:Mouse Event

Description

The which 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:

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

The following code shows how to find out which mouse button that was pressed when a mouse event was triggered:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

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

<script>
function WhichButton(event) {//from   w w  w  .  j a v a 2s. c  o  m
    console.log("You pressed button: " + event.which)
}
</script>

</body>
</html>

Related Tutorials