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:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.