Coding⏱️ 3 min read📅 2026-06-03

How to Fix: How to distinguish between left and right mouse click with jQuery

Distinguish between left and right mouse click with jQuery by using the mousebutton event.

Quick Answer: Use the mousebutton event to capture the clicked button, e.g. $('div').bind('click', function(event){ if (event.button == 0) { alert('left mouse button is pressed'); } else if (event.button == 2) { alert('right mouse button is pressed'); } });

To distinguish between left and right mouse click with jQuery, we need to use a different event handler for the 'rightclick' event. Unfortunately, jQuery does not provide a built-in 'rightclick' event. However, we can achieve this by using the 'mousedown' event and checking the mouse buttons pressed.

🔍 Why This Happens

  • jQuery does not provide a built-in 'rightclick' event.

🔧 Proven Troubleshooting Steps

Method 1: Using Mousedown Event

  1. Step 1: Bind a 'mousedown' event handler to the element.

Method 2: Using MouseButtons Property

  1. Step 1: Use the 'mousebuttons' property to check if the right mouse button is pressed.

Here's an example of how you can achieve this using both methods:

$('div').bind('mousedown', function(event) {
var buttons = event.which; // 0 for left, 2 for right
if (buttons == 2) { // check for right mouse button
alert('Right mouse button is pressed');
} else { // left mouse button
alert('Left mouse button is pressed');
} }).bind('mouseup', function(event) {
var buttons = event.which; // 0 for left, 2 for right
if (buttons == 2) { // check for right mouse button
alert('Right mouse button is released');
} else { // left mouse button
alert('Left mouse button is released');
} });

Another way to achieve this using the 'mousebuttons' property:

$('div').bind('mousedown', function(event) {
var buttons = event.mousebuttons; // 0 for left, 2 for right
if (buttons == 2) { // check for right mouse button
alert('Right mouse button is pressed');
} else { // left mouse button
alert('Left mouse button is pressed');
} }).bind('mouseup', function(event) {
var buttons = event.mousebuttons; // 0 for left, 2 for right
if (buttons == 2) { // check for right mouse button
alert('Right mouse button is released');
} else { // left mouse button
alert('Left mouse button is released');
} });

✨ Wrapping Up

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions