Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to handle a mouse right click event using jQuery?
To handle a mouse right click event, use the mousedown() jQuery method. Mouse left, right and middle click can also be captured using the same method. The event.which property helps identify which mouse button was clicked: 1 for left, 2 for middle, and 3 for right mouse button.
Example
You can try to run the following code to learn how to handle a mouse right click event ?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.myclass').mousedown(function(event) {
switch (event.which) {
case 1:
alert('Left mouse button is pressed');
break;
case 2:
alert('Middle mouse button is pressed');
break;
case 3:
alert('Right mouse button is pressed');
break;
default:
alert('Nothing');
}
});
});
</script>
</head>
<body>
<a href="http://qries.com" class="myclass">Click me</a>
</body>
</html>
The output of the above code is ?
When you left-click the link: "Left mouse button is pressed" alert appears When you right-click the link: "Right mouse button is pressed" alert appears When you middle-click the link: "Middle mouse button is pressed" alert appears
Conclusion
The mousedown() method in jQuery provides an effective way to handle right-click events by checking the event.which property. This approach allows you to distinguish between different mouse buttons and execute specific actions for each click type.
Advertisements
