JavaScript | MouseEvent Button Property
The MouseEvent button property is used to define the left or right click events. When mouse button is clicked then it return an integer value which describe the left, right or middle mouse button.
Syntax:
event.button
Return Value: This event return an integer value on mouse click events are:
- 0: It indicates the left mouse button.
- 1: It indicates the middle mouse button.
- 2: It indicates the right mouse button.
The onmousedown event: This event occurs when a user presses a mouse button over an element.
Program 1:
<!DOCTYPE html> <html> <head> <title>JavaScript Mouse Event</title> <style> body { text-align:center; } .gfg { font-size:40px; font-weight:bold; color:green; } </style> </head> <body> <div class = "gfg">GeeksforGeeks</div> <h2>Mouse click event</h2> <button onclick="click(event)">Click me</button> <p id="demo"></p> <script> document.onmousedown = click // click function called function click(event) { // Condition to disable left click if (event.button == 0) { alert("Left click not allowed"); } } </script> </body> </html> |
Output:

Program 2:
<!DOCTYPE html> <html> <head> <title>JavaScript Mouse Event</title> <style> body { text-align:center; } .gfg { font-size:40px; font-weight:bold; color:green; } </style> </head> <body> <div class = "gfg">GeeksforGeeks</div> <h2>Mouse click event</h2> <button onclick="click(event)">Click me</button> <p id="demo"></p> <script> document.onmousedown = click // click function called function click(event) { // Condition to disable left click if (event.button == 2) { alert("Right click not allowed"); } } </script> </body> </html> |
Output:

Recommended Posts:
- Javascript | MouseEvent which Property
- Javascript | MouseEvent ctrlKey Property
- JavaScript | MouseEvent shiftKey Property
- Javascript | MouseEvent altKey Property
- Javascript | MouseEvent getModifierState() Method
- HTML | DOM MouseEvent screenY Property
- HTML | DOM MouseEvent relatedTarget Property
- HTML | DOM MouseEvent clientX Property
- HTML | DOM MouseEvent screenX Property
- HTML | DOM MouseEvent offsetY Property
- HTML | DOM MouseEvent pageY Property
- HTML | DOM MouseEvent pageX Property
- HTML | DOM MouseEvent clientY Property
- HTML | DOM MouseEvent offsetX Property
- JavaScript | Trigger a button on ENTER key
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.



