Open In App

Javascript MouseEvent which Property

The mouse event which property is used to return a number that corresponds to the pressed mouse button when a mouse event is triggered Syntax:

event.which

Return value: It returns a number indicating which mouse button is pressed:



Example: 




<!DOCTYPE html>
<html>
  
<head>
    <title>mouseEvent which Property</title>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
  
    <h2>
        mouseEvent which Property
    </h2>
  
    <button onmousedown="geek(event)">Click me!</button>
  
    <p id="p"></p>
    <script>
        function geek(event) {
            if (event.which == 1) {
                document.getElementById('p').innerHTML = "Left mouse"
                                            + " button is clicked ";
            } else if (event.which == 2) {
                document.getElementById('p').innerHTML = "Middle mouse"
                                            + " button is clicked ";
            } else if (event.which == 3) {
                document.getElementById('p').innerHTML = "Right mouse"
                                                + "button is clicked ";
            }
        }
    </script>
</body>
  
</html>

Output: 



 

Supported Browsers: The browser supported by which mouse events are listed below:


Article Tags :