jQuery

jQuery Detect Mouse (Left Click Right Click Middle Button Click)

Did you ever use jQuery to Detect Mouse Click on a div or any other element using jQuery? In this article, I am going to discuss a simple and easy way to detect mouse click i.e. you will be able to detect left click, detect right click & detect mouse middle button click using jQuery.

Examples to Detect Mouse Click ( Left Click, Right Click, Middle Button Click) Using jQuery

Let’s assume that you have Div with ID “my_div” and you want to detect the mouse click on this div. In this case, it’s possible to do the following:

  1. Detect Left Click using jQuery
  2. Detect Middle Button Click using jQuery
  3. Detect Right Click using jQuery

Example: HTML + jQuery Code for mouse click detection

//HTML Code
<div id="my_div" title="This is title">This is a test div</div>

//jQuery Code
$('#my_div').mousedown(function(event) {
switch (event.which) {
case 1:
alert('Left mouse button has been pressed.');
break;
case 2:
alert('Middle mouse button has been pressed.');
break;
case 3:
alert('Right mouse button has been pressed.');
break;
default:
alert('Invalid request.');
}
});
Simple, isn’t it?

Do you know of any other ways to detect mouse click using jQuery? Feel free to suggest by commenting below.

Share your thoughts, comment below now!

*

*