Open In App

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 the right mouse button double clicks. 

In this article, we will see how to simulate the double right mouse click in jQuery.



Approach: 

Example 1: In this example, we are using the above-explained approach.






<!DOCTYPE html>
<html>
<head>
    <!-- jQuery -->
    <script src=
    </script>
 
    <!-- Basic inline styling -->
    <style>
        body {
            text-align: center;
        }
 
        h1 {
            color: green;
            font-size: 2.5rem;
        }
 
        div {
            font-weight: bold;
            font-size: 1.5rem;
        }
 
        #target {
            cursor: pointer;
            margin-bottom: 2rem;
        }
 
        #result {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <div id="target">Right click here to see effect</div>
    <div id="result"></div>
    <script type="text/javascript">
        // Suppress the right-click menu
        $("#target").on("contextmenu", function (event) {
            event.preventDefault();
        });
 
        $("#target").mouseup(function (event) {
            if (event.which === 3) {
                if (event.originalEvent.detail === 2) {
                    $("#result").text("Double right-click!");
                } else if (event.originalEvent.detail === 1) {
                    $("#result").text("Single right-click!");
                }
            }
        });
    </script>
</body>
</html>

Output:

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.




<!DOCTYPE html>
<html>
<head>
    <!-- jQuery -->
    <script src=
    </script>
 
    <!-- Basic inline styling -->
    <style>
        body {
            text-align: center;
        }
 
        h1 {
            color: green;
            font-size: 2.5rem;
        }
 
        div {
            font-weight: bold;
            font-size: 1.5rem;
        }
 
        #target {
            cursor: pointer;
            margin-bottom: 2rem;
        }
 
        #result {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <div id="target">Right click here to see effect</div>
    <div id="result"></div>
    <script type="text/javascript">
        // Suppress the right-click menu
        $("#target").on("contextmenu", function (event) {
            event.preventDefault();
        });
 
        $("#target").mouseup(function (event) {
            if (event.which === 3) {
                if (event.originalEvent.detail === 2) {
                    $("#result").append("Double right-click!" + "<br />");
                }
            }
        });
    </script>
</body>
</html>

Output:


Article Tags :