The .contextmenu() method is jQuery’s legacy shorthand for binding and triggering the contextmenu event. This tutorial covers .contextmenu(handler) since 1.0, .contextmenu(eventData, handler) since 1.4.3, no-argument .contextmenu() as a trigger, migration to .on("contextmenu") and .trigger("contextmenu"), and why the binding form is deprecated since jQuery 3.3. For the modern right-click API, see the jQuery contextmenu event tutorial.
01
.contextmenu(fn)
Bind handler
02
eventData
Since 1.4.3
03
.contextmenu()
Trigger event
04
.on()
Modern bind
05
.trigger()
Modern fire
06
Deprecated
Since 3.3
Fundamentals
Introduction
Before jQuery 1.7 unified events with .on() and .off(), every common event had a matching shorthand — .click(), .contextmenu(), .keydown(), and dozens more. The .contextmenu() method let you bind a right-click handler in one short call: $("#panel").contextmenu(function(e){ ... }). It remains in jQuery 3.x for backward compatibility but is marked deprecated.
Understanding .contextmenu() matters when reading older tutorials and legacy codebases that handle right-clicks. The method does two distinct jobs: with a function argument it binds; with no arguments it triggers. Modern jQuery splits those roles explicitly — .on("contextmenu", handler) for binding and .trigger("contextmenu") for firing. This page documents the shorthand API and shows how to migrate.
Concept
Understanding the .contextmenu() Method
The official jQuery API describes .contextmenu() as a way to bind an event handler to the contextmenu event, or trigger that event on an element. When you pass a handler, jQuery registers it on each matched element and calls it when the user right-clicks (or presses the context-menu key) — before the browser’s default menu appears — the same contextmenu event that .on("contextmenu") handles today.
When you call .contextmenu() with no arguments, jQuery synthetically fires the contextmenu event on each matched element. Bound handlers run. This trigger behavior is equivalent to .trigger("contextmenu").
⚠️
Deprecated since jQuery 3.3
Do not use .contextmenu(handler) or .contextmenu(eventData, handler) in new code. Replace them with .on("contextmenu", handler) or .on("contextmenu", eventData, handler). Replace no-argument .contextmenu() with .trigger("contextmenu"). For preventDefault(), custom menus, and the full modern guide, read the jQuery contextmenu event tutorial.
Foundation
📝 Syntax
The .contextmenu() method has three signatures from the official jQuery API. The binding forms are deprecated; the trigger form remains valid but .trigger("contextmenu") is preferred:
All .contextmenu() signatures return the original jQuery object for chaining.
Returning false from a handler is equivalent to preventDefault() + stopPropagation() — useful for blocking the native menu in legacy code.
Cheat Sheet
⚡ Quick Reference
Goal
Legacy (.contextmenu)
Modern replacement
Bind right-click handler
$("#box").contextmenu(fn) ⚠
$("#box").on("contextmenu", fn)
Pass data to handler
$("#box").contextmenu({ id: 1 }, fn) ⚠
$("#box").on("contextmenu", { id: 1 }, fn)
Block browser menu
event.preventDefault() inside handler (both APIs)
Trigger programmatically
$("#target").contextmenu()
$("#target").trigger("contextmenu")
Remove handlers
$("#box").off("contextmenu")
Delegated contextmenu on children
$("#list").on("contextmenu", "li", fn) — not available via .contextmenu()
One-time handler
$("#box").one("contextmenu", fn) — use .one(), not .contextmenu()
Compare
📋 .contextmenu() vs .on("contextmenu") vs .trigger("contextmenu") vs .click()
Four related APIs — know which handles right-click, which is modern, and which covers left-click only.
.contextmenu()
deprecated
Legacy bind (.contextmenu(fn)) or trigger (.contextmenu()) — migrate for new code
.on("contextmenu")
bind
Modern binding since 1.7 — supports eventData, delegation, and preventDefault
.trigger("contextmenu")
fire
Programmatically run handlers — clearer than no-arg .contextmenu()
.click()
left only
Separate deprecated shorthand for left-click — does not handle right-click
Hands-On
Examples Gallery
Five examples cover binding, eventData, triggering, migration from .contextmenu() to .on("contextmenu"), and method chaining. Use the Try-it links and right-click inside the demo area. Remember: binding with .contextmenu(handler) is deprecated — these demos show legacy syntax you will encounter in older code.
📚 Binding & Triggering
Legacy .contextmenu() signatures for binding handlers and firing events programmatically.
Example 1 — Bind Handler with .contextmenu(handler)
The canonical legacy pattern — attach a function that runs when the user right-clicks the element.
jQuery
$( "#target" ).contextmenu( function() {
$( "#out" ).text( "Handler ran via .contextmenu(function(){ ... })." );
} );
User right-clicks #target → handler runs
#out text updates to confirm .contextmenu(fn) fired
Browser menu may still appear unless preventDefault() is called
Deprecated — prefer .on("contextmenu", fn) for new code
How It Works
.contextmenu(fn) registers the function on the element. jQuery internally routes this to .on("contextmenu", fn). The handler runs when the browser fires a right-click — before the native context menu opens.
Example 2 — Pass eventData with .contextmenu(eventData, handler)
Since jQuery 1.4.3, pass an object before the handler — it arrives as event.data.
Right-click File A → "Right-clicked: File A | eventData.menu: actions"
Right-click File B → same eventData.menu on every .item element
Modern equivalent: .on("contextmenu", { menu: "actions" }, fn)
How It Works
jQuery stores the { menu: "actions" } object and injects it into event.data when any matched .item is right-clicked. Use $(this).data("label") for per-element attributes.
Example 3 — Trigger Contextmenu with No-Argument .contextmenu()
Calling .contextmenu() without a handler fires the contextmenu event programmatically.
The first .contextmenu(function(){ ... }) binds a handler on #target. The left-click on #fire calls $("#target").contextmenu() with no arguments — that synthetically fires the contextmenu event. Replace with $("#target").trigger("contextmenu") for clarity.
📈 Migration & Chaining
Compare legacy and modern APIs, and chain after .contextmenu(handler).
Example 4 — Migration: .contextmenu(fn) vs .on("contextmenu", fn)
Both bind the same contextmenu event — .on() is the recommended API for new code.
jQuery
$( "#legacy" ).contextmenu( function() {
$( "#out" ).text( "Legacy: .contextmenu(function(){ ... }) — deprecated since jQuery 3.3." );
} );
$( "#modern" ).on( "contextmenu", function() {
$( "#out" ).text( "Modern: .on('contextmenu', function(){ ... }) — use this for new code." );
} );
Right-click #legacy → legacy deprecation message
Right-click #modern → modern .on("contextmenu") message
Behavior is identical — only the API name differs
How It Works
Under the hood, .contextmenu(fn) calls .on("contextmenu", fn). Migration is a straight rename with no behavior change for simple bindings. The advantage of .on() is delegation — $("#list").on("contextmenu", "li", fn) — which .contextmenu() cannot do.
Example 5 — Chaining After .contextmenu(handler)
.contextmenu(handler) returns the jQuery object — chain further methods immediately. Block the native menu with preventDefault().
Right-click #panel → #out updates, browser menu suppressed
.addClass("has-menu") runs once at setup — not on each right-click
Same chaining works with .on("contextmenu", fn).addClass("has-menu")
How It Works
.contextmenu(fn) returns the jQuery collection, so .addClass("has-menu") runs immediately during initialization. Inside the handler, event.preventDefault() stops the browser’s default context menu — essential when building custom menus.
Applications
🚀 Common Use Cases
Reading legacy code — recognize $("#panel").contextmenu(fn) as equivalent to .on("contextmenu", fn) when maintaining older projects.
Custom context menus — legacy apps often used .contextmenu(function(e){ e.preventDefault(); ... }) — migrate to .on("contextmenu") with the same preventDefault() pattern.
Stacked handlers — multiple .contextmenu(fn) calls on one element run in registration order — same as multiple .on("contextmenu", fn) calls.
Automated testing — test suites may call .contextmenu() to simulate right-clicks — consider .trigger("contextmenu") for explicit intent.
🧠 How .contextmenu() Routes Through jQuery
1
You call .contextmenu()
With a function argument, jQuery treats it as a bind request. With no arguments, it treats it as a trigger request.
dispatch
2
Bind path
.contextmenu(handler) delegates internally to .on("contextmenu", handler) — the handler is stored in jQuery’s event system.
.on("contextmenu")
3
Trigger path
No-arg .contextmenu() delegates to .trigger("contextmenu") — bound handlers run on each matched element.
.trigger("contextmenu")
4
📋
jQuery object returned
Both paths return the original collection for chaining — .contextmenu(fn).addClass("ready") works the same as .on("contextmenu", fn).addClass("ready").
Important
📝 Notes
Deprecated since jQuery 3.3 — do not use .contextmenu(handler) or .contextmenu(eventData, handler) in new code. Use .on("contextmenu") instead.
Bind with .contextmenu(handler) since jQuery 1.0; eventData form since 1.4.3.
No-argument .contextmenu() triggers the event since jQuery 1.0 — prefer .trigger("contextmenu").
.contextmenu() does not support event delegation — use .on("contextmenu", selector, fn) for dynamic lists.
Call event.preventDefault() inside the handler to block the browser’s native menu — works with both legacy and modern APIs.
Left-click handlers (.click() or .on("click")) do not run on right-click — bind contextmenu separately.
The underlying contextmenu event is a standard DOM event supported in every browser jQuery targets. The .contextmenu() method itself is a jQuery API — it works in jQuery 1.x, 2.x, and 3.x. Binding via .contextmenu(handler) is deprecated since 3.3 but still functional; triggering via no-arg .contextmenu() also remains supported.
✓ jQuery 1.0+
jQuery .contextmenu() method
Supported across all jQuery versions you are likely to maintain. Deprecated binding since 3.3 — use .on('contextmenu') for new projects. Native equivalent for binding: element.addEventListener('contextmenu', fn).
100%With jQuery loaded
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
.contextmenu()Legacy API
Bottom line: Safe in legacy jQuery projects. Migrate bindings to .on('contextmenu') and triggers to .trigger('contextmenu') when updating code. Use preventDefault() to block the native menu.
Wrap Up
Conclusion
The jQuery .contextmenu() method is the legacy shorthand for binding and triggering right-click handlers. Use .contextmenu(handler) or .contextmenu(eventData, handler) to bind — both deprecated since jQuery 3.3. Use no-argument .contextmenu() to trigger — equivalent to .trigger("contextmenu").
For new code, prefer .on("contextmenu", fn) for binding and .trigger("contextmenu") for programmatic firing. When you encounter .contextmenu() in older projects, recognize it, understand it, and migrate when practical. For custom menus, preventDefault(), and the complete modern workflow, continue with the jQuery contextmenu event tutorial.
Write new code with deprecated .contextmenu(handler)
Assume .click(handler) handles right-clicks — it does not
Assume .contextmenu() supports delegation — it does not
Confuse .contextmenu(fn) (bind) with .contextmenu() (trigger)
Copy deprecated patterns from old tutorials without modernizing them
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .contextmenu()
Legacy bind, modern migrate.
6
Core concepts
.ctx01
.contextmenu(fn)
Bind
Deprecated
data02
eventData
Since 1.4.3
Handler
()03
.contextmenu()
Trigger
No args
.on04
.on("contextmenu")
Modern
Replace
⚡05
.trigger()
Fire
Programmatic
3.306
Deprecated
Since 3.3
Migrate
❓ Frequently Asked Questions
The .contextmenu() method has two roles. With a handler argument — .contextmenu(handler) or .contextmenu(eventData, handler) — it binds a function that runs when the user right-clicks the matched element (before the browser menu appears). With no arguments — .contextmenu() — it triggers the contextmenu event on each matched element. Both forms have been part of jQuery since 1.0; the binding form is deprecated since 3.3.
Yes — the binding signatures .contextmenu(handler) and .contextmenu(eventData, handler) are deprecated since jQuery 3.3. Use .on('contextmenu', handler) or .on('contextmenu', eventData, handler) instead. The no-argument .contextmenu() trigger form still works but .trigger('contextmenu') is clearer and preferred in modern code. Existing legacy code continues to run in jQuery 3.x; migrate when you touch those files.
.contextmenu(handler) registers a callback — it does not fire the event immediately. .contextmenu() with no arguments triggers the contextmenu event programmatically on every matched element, the same as .trigger('contextmenu'). The handler form is deprecated for binding; the no-arg form is a trigger shorthand. Always check whether parentheses contain a function before reading legacy jQuery code.
Replace .contextmenu(fn) with .on('contextmenu', fn). Replace .contextmenu(data, fn) with .on('contextmenu', data, fn). Replace no-arg .contextmenu() with .trigger('contextmenu'). Replace .unbind('contextmenu') cleanup with .off('contextmenu'). Under the hood, .contextmenu(handler) already delegates to .on('contextmenu') — migration is a straight rename with no behavior change for simple bindings.
Yes. jQuery 3.x keeps .contextmenu() for backward compatibility with jQuery 1.x and 2.x codebases. It still binds and triggers correctly. The API docs mark it deprecated to steer new projects toward .on() and .trigger(). Do not use .contextmenu(handler) in new code — prefer the modern API covered in the jQuery contextmenu event tutorial.
Since jQuery 1.4.3, you can pass an object as the first argument before the handler. jQuery attaches it to event.data inside the callback — for example, .contextmenu({ action: 'copy' }, fn) lets fn read event.data.action. This avoids closures when the same handler serves multiple elements. The modern equivalent is .on('contextmenu', { action: 'copy' }, fn).
Did you know?
jQuery deprecated event shorthand methods in stages — .click(handler) and .contextmenu(handler) were both marked deprecated in version 3.3, while the unified .on() API arrived much earlier in 1.7. Under the hood, .contextmenu(fn) has always been a thin wrapper around .on("contextmenu", fn), so migrating legacy right-click code is often a one-line rename with no runtime behavior change.