.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.
Optional data (1.4.3+): .contextmenu({ key: "v" }, handler) → event.data.
Trigger (legacy): .contextmenu() with no args — or use .trigger("contextmenu").
Piece
Required?
Role
handler
For bind
Runs on right-click / menu key
eventData
Optional
Available 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
Reference
Cheat Sheet
Quick map from shorthand to modern calls.
Legacy
Modern
.contextmenu(fn)
.on("contextmenu", fn)
.contextmenu(data, fn)
.on("contextmenu", data, fn)
.contextmenu()
.trigger("contextmenu")
Unbind
.off("contextmenu")
Added
1.0 (data form 1.4.3)
Status
Deprecated 3.3+
Try it
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).
Code
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).");
});
.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")
});
.contextmenu(fn) is a thin wrapper around .on("contextmenu", fn).
.on supports delegation and is not deprecated.
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.
Compatibility
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.
Switching to .on / .trigger does not slow you down — it clarifies intent and unlocks delegation.
Remember
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).