How to add dbclick() on right click in jQuery?
The jQuery dblclick() method is used to detect whenever a particular element is double-clicked. This method only detects left mouse button double clicks and not right mouse button double clicks.
In this article, we will see how to simulate the double right mouse click in jQuery.
- There are two HTML div elements, one with an id target and the other with an id result.
- Attach an event handler to the target id by using the on() method. The event name is contextmenu which simply suppresses the right-click menu as we want to prevent the default action of right-clicking using the preventDefault() method.
- The mouseup() method is attached to the target id element to detect the event when the user releases a mouse button over this element. An anonymous function with a parameter event is passed as a parameter to the mouseup() method such that we can utilize the which, originalEvent, and the detail properties of the event object.
- The which property is used to check which mouse button is clicked, left or right It returns 3 if it is a right-click, so we use this as a base condition.
- We use the originalEvent and the detail properties of the event object to check whether it is a single right-click or a double right-click. These properties return 2 if it is a double right-click else returns 1. The result id element displays this single or double right-click using the text() method.
Example 2: In this example, we are only checking for a double right click instead of both a single and a double right-click. It means only the condition event.originalEvent.detail === 2 is checked. Moreover, we append the text every time on double right click instead of simply displaying it once so that we can see exactly how many doubles right-click have been performed. We have performed the double right-click operation exactly 5 times.