jQuery contextmenu() Method

Last Updated:

jQuery contextmenu() Method

The jQuery contextmenu() method is used for the following 2 purpose

  1. To run a callback when a specified element is right clicked.
  2. To trigger the contextmenu event of a specified element.

Run a Callback

In the following example, we will run a callback function when a paragraph is right clicked.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery contextmenu() Method</h1> <p>Right click this paragraph</p> <script> $(document).ready(function(){ $("p").contextmenu(function(){ alert("You right clicked."); }); }); </script> </body> </html>

Syntax

$(selector).contextmenu(func);

Parameter Values

Value Type Explanation
func Optional Specifies the function to run when the contextmenu event occurs for the specific elements.

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
contextmenu()YesYesYesYesYesYesYesYesYesYesYesYesYes

Trigger a contextmenu Event

In the following example, we will trigger a contextmenu event when a button is clicked.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery contextmenu() Method</h1> <p>Right click this paragraph</p> <button>Trigger contextmenu Event</button> <script> $(document).ready(function(){ $("p").contextmenu(function(){ alert("You right clicked."); }); $("button").click(function(){ $("p").contextmenu(); }); }); </script> </body> </html>

Share this Page

Meet the Author