Open In App
Related Articles

How to distinguish left and right mouse click using jQuery?

Improve Article
Save Article
Like Article

jQuery can be used to distinguish which mouse button is being clicked. This can be used in various web applications where knowing a certain mouse button can be used to trigger certain functionality.

Using the mousedown() method:

The mousedown() method in jQuery can be used to bind an event handler to the default ‘mousedown’ JavaScript event. This can be used to trigger an event. The event object’s ‘which’ property can then used to check the respective mouse button.


$( "#target" ).mousedown(function() {
  alert( "Function to be handles here" );

“which” property:
The which property has 3 values depending on the mouse button pressed. We can use a switch case to get the corresponding value.

  • 1 for the left mouse button
  • 2 for the middle mouse button
  • 3 for the right mouse button


<!DOCTYPE html>
<html lang="en">
      How to distinguish left and right 
      mouse click using jQuery?
    <h1 style="color: green">
      How to distinguish left and 
      right mouse click using jQuery?
    <p>You are pressing the :
        <div class="output">
      Click Here to check the mousebutton pressed
    <script type="text/javascript">
        $('button').mousedown(function(event) {
            switch (event.which) {
                case 1:
                    document.querySelector('.output').innerHTML =
                        'Left Mouse Button';
                case 2:
                    document.querySelector('.output').innerHTML =
                        'Middle Mouse Button';
                case 3:
                    document.querySelector('.output').innerHTML =
                        'Right Mouse Button';

Clicking with the left mouse button:

Clicking with the middle mouse button:

Clicking with the right mouse button:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks 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!

Last Updated : 22 Apr, 2019
Like Article
Save Article
Similar Reads
Complete Tutorials