jQuery Contextmenu Event

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is the contextmenu Event?

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()
NeedUse
Bind handler.on("contextmenu", handler)
Block browser menuevent.preventDefault()
Fire event.trigger("contextmenu")
Left clickclick

How to Use contextmenu

Select an element, bind with .on, optionally block the native menu.

  1. Select the element(s): $("#panel"), $("p"), etc.
  2. Bind: .on("contextmenu", function (event) { ... }).
  3. Optional: event.preventDefault() to hide the browser menu.
  4. To fire in code: .trigger("contextmenu"). Unbind with .off("contextmenu").
PieceRequired?Role
"contextmenu"YesEvent type for .on / .trigger
handlerYes (bind)Runs on right-click / menu key
eventDataOptionalExtra 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

Cheat Sheet

Keep these facts handy while wiring right-click handlers.

TopicDetail
Bind.on("contextmenu", handler)
Trigger.trigger("contextmenu") (1.0+)
TimingBefore the native context menu
Block menuevent.preventDefault()
Delegation.on("contextmenu", "li", fn)
Deprecated.contextmenu() shorthand (3.3+)

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.

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.");
});
Try It Yourself

How It Works

  1. Select #target.
  2. Bind with .on("contextmenu", handler).
  3. User right-clicks the element.
  4. Handler runs before the browser menu appears.

Example 2 — Block the Browser Menu

Call preventDefault() so the native menu does not open.

jQuery
$("#target").on("contextmenu", function (event) {
  event.preventDefault();
  console.log("Custom action — native menu blocked.");
});
Try It Yourself

How It Works

  1. Bind contextmenu on #target.
  2. preventDefault() cancels the default menu.
  3. Your code can show a custom menu or message instead.
  4. 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");
});
Try It Yourself

How It Works

  1. Bind the real handler on #target.
  2. #fire listens for a normal click.
  3. trigger("contextmenu") runs #target’s handlers.
  4. Native menu may not appear for synthetic triggers — handlers still run.

Example 4 — Read pageX / pageY

Use pointer coordinates to place a custom menu later.

jQuery
$("#target").on("contextmenu", function (event) {
  event.preventDefault();
  console.log("x:", event.pageX, "y:", event.pageY);
});
Try It Yourself

How It Works

  1. Right-click fires contextmenu with a mouse event.
  2. pageX / pageY are document coordinates of the pointer.
  3. preventDefault keeps the native menu closed.
  4. 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
on("contextmenu") Excellent

Bottom line: Bind with .on("contextmenu", fn). Block the menu with preventDefault(). Trigger with .trigger("contextmenu").

Performance Notes

ApproachCostBest for
Direct .on("contextmenu")1 handler / elementFew static panels
Delegated .on1 handler / parentLists / grids / dynamic UI
Many direct bindsHigher memoryAvoid on large collections

Right-clicks are rare compared to mousemove — focus on clear UX and .off when removing long-lived widgets.

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.

Next: .contextmenu() shorthand

See the deprecated helper and how it maps to on / trigger.

.contextmenu() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful