Skip to main content
knackforge blog knowledge base

Responding to various mouse clicks with jQuery

Mouse has been the primary medium of input for decades. Normally there are three types of mouse click. They are left, right and middle click.

In jQuery, we all knew there is a click() method acts as event handler to capture this action. 

$(selector).click();

But how to capture a specific click. For example, if we want to make some change only on left click or only on right click. 

Of course, the common click() handler will not help us in this case. So we need to move for targeting specfic click event.

But how to capture this specific click action. Is it possible? Yes it is with below code snippet,

$(selector).mousedown(function(e){
  if (e.which == 1) {
    // this is left click event.
  }
  if (e.which == 2) {
    // this is middle click event.
  }
  if (e.which == 3) {
    // this is right click event.
  }
});

With mousedown() method we can capture the specific clicks. 

This works on majority of browsers like Chrome, Firefox, IE and Safari. By capturing the code value returned by e.which we can determine the specific click. Hope this blog may help you.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.