contextmenu is sent when the user right-clicks an element — before the browser context menu appears — or when the context-menu key is pressed. Bind with .on("contextmenu", handler).
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("#target").on("contextmenu", function (event) { ... })
$("#target").trigger("contextmenu")
// Fires before the native menu
// Use event.preventDefault() to block that menu
// Prefer on/trigger — not deprecated .contextmenu()
Call
What it does
Contextmenu
Learn the jQuery contextmenu event: bind right-click with .on(\
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Select the element(s): $("#panel"), $("p"), etc.
Step 2
Bind: .on("contextmenu", function (event) { ... }).
Step 3
Optional: event.preventDefault() to hide the browser menu.
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
jQuery Examples
Example 1 — Bind a Contextmenu Handler
Official style: .on("contextmenu", …) on a target.
jQuery
$("#target").on("contextmenu", function () {
console.log("Handler for contextmenu called.");
});
Your code can show a custom menu or message instead.
Example 3 — Trigger Contextmenu from Code
Fire the same handlers without a real right-click.
jQuery
$("#target").on("contextmenu", function () {
console.log("Handler for contextmenu called.");
});
$("#fire").on("click", function () {
$("#target").trigger("contextmenu");
});
contextmenu fires when the user right-clicks an element (before the browser menu appears), or when the context-menu key is pressed on the focused element. Bind with .on("contextmenu", handler).
Call event.preventDefault() in your handler. Without it, the native menu still opens after your code runs.
Prefer .on("contextmenu", handler). The .contextmenu() shorthand is deprecated since jQuery 3.3. Trigger with .trigger("contextmenu").
click is the left-button press-and-release sequence. contextmenu is a separate right-click (or menu-key) event. Binding click does not handle right-clicks.
Use $(selector).trigger("contextmenu"). That runs bound handlers. Synthetic triggers may not always show the native browser menu — test in your browsers.
Yes. The browser fires contextmenu on the html element or the focused element. Your .on("contextmenu") handler runs the same way as for a mouse right-click.