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).
Remember
$("#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()
Select an element, bind with .on, optionally block the native menu.
Select the element(s): $("#panel"), $("p"), etc.
Bind: .on("contextmenu", function (event) { ... }).
Optional: event.preventDefault() to hide the browser menu.
To fire in code: .trigger("contextmenu"). Unbind with .off("contextmenu").
Piece
Required?
Role
"contextmenu"
Yes
Event type for .on / .trigger
handler
Yes (bind)
Runs on right-click / menu key
eventData
Optional
Extra data → event.data
Return value
—
jQuery set (chainable)
Pseudocode
Pseudocode
function onContextmenu(element, handler):
when right-click on element
(or context-menu key while focused):
before browser menu shows:
handler(event)
trigger(element, "contextmenu"):
run all contextmenu handlers on element
Reference
Cheat Sheet
Keep these facts handy while wiring right-click handlers.
Topic
Detail
Bind
.on("contextmenu", handler)
Trigger
.trigger("contextmenu") (1.0+)
Timing
Before the native context menu
Block menu
event.preventDefault()
Delegation
.on("contextmenu", "li", fn)
Deprecated
.contextmenu() shorthand (3.3+)
Try it
Live Preview
Run presets that simulate on("contextmenu") (via .trigger so demos work without a real right-click).
Uses .on("contextmenu") and .trigger("contextmenu") — try the editors below for a real right-click.
Result
Handler for contextmenu called.
Code
jQuery Examples
Short, copy-ready snippets. Right-click in Try It Yourself, or use View Output for the expected text.
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.
Without it, the browser menu still appears.
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");
});
pageX / pageY are document coordinates of the pointer.
preventDefault keeps the native menu closed.
Use coords to position your own UI at the click point.
Edge Cases & Pitfalls
Watch these when handling right-clicks.
deprecated
Avoid .contextmenu() shorthand
Deprecated in 3.3. Use .on("contextmenu") / .trigger("contextmenu") / .off("contextmenu").
default
Menu still shows without preventDefault
Your handler runs, then the browser menu opens unless you call event.preventDefault().
click
click ≠ contextmenu
Left-click handlers never see right-clicks. Bind both events if you need both behaviors.
trigger
Synthetic trigger ≠ real menu
.trigger("contextmenu") runs handlers; the native menu may not appear. Test real right-clicks for UX.
Compatibility
Browser Support
The native contextmenu event works in all modern browsers. Bind with .on("contextmenu"); .trigger("contextmenu") since jQuery 1.0. Prefer these over the deprecated .contextmenu() shorthand (3.3+).
✓ jQuery 1.0+
contextmenu event
Supported everywhere jQuery event APIs run in the browser. Use on/trigger for modern code.
100%Browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
on("contextmenu")Excellent
Bottom line: Bind with .on("contextmenu", fn). Block the menu with preventDefault(). Trigger with .trigger("contextmenu").
Analysis
Performance Notes
Approach
Cost
Best for
Direct .on("contextmenu")
1 handler / element
Few static panels
Delegated .on
1 handler / parent
Lists / grids / dynamic UI
Many direct binds
Higher memory
Avoid on large collections
Right-clicks are rare compared to mousemove — focus on clear UX and .off when removing long-lived widgets.
Remember
Key Takeaways
Bind:.on("contextmenu", handler) — not deprecated .contextmenu().
Block menu: call event.preventDefault() when needed.
Fire:.trigger("contextmenu") for programmatic calls.
Separate from click: right-click is its own event.
One line: use .on("contextmenu", fn) for right-clicks, and preventDefault() when you replace the browser menu.
Frequently Asked Questions
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.