Related Articles

Related Articles

JavaScript | MouseEvent Button Property
  • Difficulty Level : Hard
  • Last Updated : 19 Feb, 2019

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!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


Output:
right click

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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


Output:
right click

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :