jQuery .contextmenu() Method

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

What Is the .contextmenu() Method?

.contextmenu() is a shorthand for binding or triggering the contextmenu event. With a handler it binds; with no arguments it triggers. Prefer .on / .trigger in new code.

Remember
$("#panel").contextmenu(fn)           // bind (deprecated)
$("#panel").contextmenu(data, fn)     // bind + eventData (deprecated)
$("#panel").contextmenu()             // trigger (prefer .trigger)

// Modern:
$("#panel").on("contextmenu", fn)
$("#panel").trigger("contextmenu")
FormMeansPrefer
.contextmenu(handler)Bind.on("contextmenu", handler)
.contextmenu(data, handler)Bind + data.on("contextmenu", data, handler)
.contextmenu()Trigger.trigger("contextmenu")

How to Use .contextmenu()

Know the three signatures, then map them to the modern API.

  1. Select element(s): $("#panel").
  2. Bind (legacy): .contextmenu(function (event) { ... }).
  3. Optional data (1.4.3+): .contextmenu({ key: "v" }, handler) → event.data.
  4. Trigger (legacy): .contextmenu() with no args — or use .trigger("contextmenu").
PieceRequired?Role
handlerFor bindRuns on right-click / menu key
eventDataOptionalAvailable as event.data
No args—Triggers contextmenu on the set
Return value—jQuery set (chainable)

Pseudocode

Pseudocode
function contextmenu(element, handler?):
    if handler given:
        bind "contextmenu" → handler   // like .on("contextmenu", handler)
    else:
        trigger "contextmenu"          // like .trigger("contextmenu")

// Prefer naming the event explicitly with on / trigger

Cheat Sheet

Quick map from shorthand to modern calls.

LegacyModern
.contextmenu(fn).on("contextmenu", fn)
.contextmenu(data, fn).on("contextmenu", data, fn)
.contextmenu().trigger("contextmenu")
Unbind.off("contextmenu")
Added1.0 (data form 1.4.3)
StatusDeprecated 3.3+

Live Preview

Run presets that show bind, eventData, and no-arg trigger via .contextmenu().

Legacy demos for reading old code — prefer .on / .trigger in new projects.

Result
Handler ran via .contextmenu(fn).

jQuery Examples

Short snippets for legacy .contextmenu(). Prefer the modern forms in new work — use Try It Yourself to experiment.

Example 1 — Bind with .contextmenu(handler)

Deprecated shorthand for .on("contextmenu", handler).

jQuery
$("#target").contextmenu(function () {
  console.log("Handler ran via .contextmenu(fn).");
});
Try It Yourself

How It Works

  1. Select #target.
  2. .contextmenu(fn) registers the callback (like .on("contextmenu", fn)).
  3. User right-clicks — before the browser menu.
  4. Handler runs once per contextmenu event.

Example 2 — Pass eventData

Since 1.4.3: data object before the handler → event.data.

jQuery
$(".panel").contextmenu({ action: "copy" }, function (event) {
  console.log("action:", event.data.action);
  console.log("id:", $(this).data("id"));
});
Try It Yourself

How It Works

  1. First arg is data, second is the handler.
  2. event.data holds that object inside the callback.
  3. this is still the right-clicked element.
  4. Modern: .on("contextmenu", { action: "copy" }, fn).

Example 3 — Trigger with No Args

.contextmenu() with no arguments fires the event — prefer .trigger("contextmenu").

jQuery
$("#target").contextmenu(function () {
  console.log("Handler for contextmenu called.");
});

$("#fire").click(function () {
  $("#target").contextmenu();  // same idea as .trigger("contextmenu")
});
Try It Yourself

How It Works

  1. Bind a handler on #target.
  2. #fire listens for its own click.
  3. $("#target").contextmenu() runs #target’s handlers.
  4. Prefer $("#target").trigger("contextmenu") in new code.

Example 4 — Migrate to .on / .trigger

Side-by-side: same contextmenu, recommended API.

jQuery
// Legacy
$("#legacy").contextmenu(function () {
  console.log("via .contextmenu(fn)");
});

// Modern (use this)
$("#modern").on("contextmenu", function () {
  console.log("via .on(\"contextmenu\", fn)");
});
Try It Yourself

How It Works

  1. Both listen for the same contextmenu event.
  2. .contextmenu(fn) is a thin wrapper around .on("contextmenu", fn).
  3. .on supports delegation and is not deprecated.
  4. New projects should only use .on / .trigger / .off.

Edge Cases & Pitfalls

Common traps when reading or writing .contextmenu() code.

deprecated

Do not use in new code

Binding forms are deprecated since 3.3. Teach and write .on("contextmenu") instead.

overload

Bind vs trigger looks similar

.contextmenu(fn) binds; .contextmenu() triggers. Check the arguments before assuming.

delegation

No child-selector form

.contextmenu() cannot do .on("contextmenu", "li", fn). For dynamic lists, use .on with a selector.

default

Still need preventDefault

Shorthand or .on — call event.preventDefault() if you want to block the browser menu.

Browser Support

.contextmenu() has existed since jQuery 1.0 (eventData since 1.4.3). It still runs in jQuery 3.x browsers, but is deprecated since 3.3 — prefer .on("contextmenu") / .trigger("contextmenu").

✓ Deprecated 3.3+

.contextmenu() shorthand

Works wherever jQuery event APIs work in the browser. Use on/trigger for new 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
.contextmenu() (legacy) Excellent (deprecated)

Bottom line: Migrate: .contextmenu(fn) → .on("contextmenu", fn); .contextmenu() → .trigger("contextmenu").

Performance Notes

ApproachCostNote
.contextmenu(fn)Same as .onThin wrapper — no win
Many direct bindsHigher memoryPrefer delegated .on
No-arg .contextmenu()Same as triggerName the event with .trigger

Switching to .on / .trigger does not slow you down — it clarifies intent and unlocks delegation.

Key Takeaways

  • Deprecated: .contextmenu(handler) since jQuery 3.3.
  • Bind: use .on("contextmenu", fn) instead.
  • Fire: use .trigger("contextmenu"), not bare .contextmenu().
  • Legacy OK: still runs in 3.x — migrate when you touch the file.

One line: .contextmenu() is a deprecated shorthand — prefer .on("contextmenu") and .trigger("contextmenu").

Frequently Asked Questions

With a handler — .contextmenu(handler) or .contextmenu(eventData, handler) — it binds a right-click callback. With no arguments — .contextmenu() — it triggers contextmenu on each matched element. The binding forms are deprecated since jQuery 3.3.
Yes. Prefer .on("contextmenu", handler) or .on("contextmenu", eventData, handler) to bind, and .trigger("contextmenu") to fire. jQuery 3.x still runs .contextmenu() for compatibility.
.contextmenu(handler) only registers a callback. .contextmenu() with no arguments triggers the event (same idea as .trigger("contextmenu")).
Replace .contextmenu(fn) with .on("contextmenu", fn). Replace .contextmenu(data, fn) with .on("contextmenu", data, fn). Replace no-arg .contextmenu() with .trigger("contextmenu").
Yes for legacy code. It still works, but new code should use .on / .trigger. See the contextmenu event tutorial for the modern API.
Since 1.4.3 you can pass data before the handler. It appears as event.data inside the callback. Modern form: .on("contextmenu", eventData, handler).

Next: .hover() method

Bind mouseenter/mouseleave with one helper.

.hover() 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.

8 people found this page helpful