Different ways to handle right click in jQuery

Posted on by Chandan

In order to handling right-click in jQuery, you need to bind an event handler to right click javascript event. Here we will discuss different ways to do such jobs.

1. By using the contextmenu event. In javascript there is an event named contextmenu, which fires after the right click button is clicked and before the menu is displayed. we can attach an event handler function with this. So, it will be triggered after pressing right click. In jQuery we have a .contextmenu() function. Have a look at this example below. We have used this function on the body element.

 

 

Here you can see that the alert is executed just before the context menu. To disable the menu, you can add “return false”.

 

 

How to get clicked element and do operations on them?
By using event.target you can get the target element where the right click is done. You need to pass an argument in the context menu function, this argument will be the event object. From the event object, you can access the target element.
Here is an example :

 

 

2. Using the mousedown event. we can handle any mouse button click with the mouse-down event. For example, execute the following:

 

About Chandan

Author View all posts by Chandan →

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*