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 > |
chevron_right
filter_none
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 > |
chevron_right
filter_none
Output: