Javascript | MouseEvent which Property
The mouseEvent which property is used to return a number which 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:
- For left mouse button: 1 is returned
- For middle mouse button: 2 is returned
- For right mouse button: 3 is returned
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:
Before Clicking the button:

After clicking left mouse button:

After clicking middle mouse button:

After clicking right mouse button:

Supported Browsers: The browser supported by which mouse event are listed below:
- Apple Safari 1.0
- Google Chrome 1.0
- Firefox 1.0
- Opera 5.0
- Internet Explorer 9.0
Recommended Posts:
- Javascript | MouseEvent altKey Property
- Javascript | MouseEvent ctrlKey Property
- JavaScript | MouseEvent shiftKey Property
- JavaScript | MouseEvent Button Property
- Javascript | MouseEvent getModifierState() Method
- HTML | DOM MouseEvent screenY Property
- HTML | DOM MouseEvent offsetX Property
- HTML | DOM MouseEvent pageX Property
- HTML | DOM MouseEvent buttons Property
- HTML | DOM MouseEvent clientY Property
- HTML | DOM MouseEvent screenX Property
- HTML | DOM MouseEvent pageY Property
- HTML | DOM MouseEvent offsetY Property
- HTML | DOM MouseEvent relatedTarget Property
- HTML | DOM MouseEvent clientX Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



