HomeLanguagesJavascriptJavascript MouseEvent which Property

Javascript MouseEvent which Property

The mouse event which property is used to return a number that 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: 

html




<!DOCTYPE html>
<html>
  
<head>
    <title>mouseEvent which Property</title>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">
        neveropen
    </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: 

 

Supported Browsers: The browser supported by which mouse events are listed below:

  • Apple Safari 1.0
  • Google Chrome 1.0
  • Firefox 1.0
  • Opera 5.0
  • Internet Explorer 9.0
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS