jQuery .contextmenu() Method

Beginner
⚠️ Deprecated since 3.3
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Mouse events

What You’ll Learn

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

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.

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.

📝 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:

1. Bind a handler — .contextmenu( handler ) (since 1.0, deprecated 3.3)

jQuery
.contextmenu( handler )
  • handler — function executed each time contextmenu fires: function( event ) { ... }.
  • Returns the jQuery object for chaining.
  • Modern replacement: .on( "contextmenu", handler ).

2. Bind with eventData — .contextmenu( [eventData], handler ) (since 1.4.3, deprecated 3.3)

jQuery
.contextmenu( [eventData], handler )
  • eventData — optional object available in the handler as event.data.
  • Modern replacement: .on( "contextmenu", eventData, handler ).

3. Trigger the event — .contextmenu() (since 1.0)

jQuery
.contextmenu()
  • No arguments — triggers contextmenu on every matched element.
  • Runs bound handlers programmatically.
  • Preferred replacement: .trigger( "contextmenu" ).

Official jQuery API migration note

jQuery
// Deprecated binding
$( "#target" ).contextmenu( function() {
  alert( "Handler for `contextmenu` called." );
} );

// Modern binding
$( "#target" ).on( "contextmenu", function() {
  alert( "Handler for `contextmenu` called." );
} );

// Deprecated trigger
$( "#target" ).contextmenu();

// Modern trigger
$( "#target" ).trigger( "contextmenu" );

Return value

  • 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.

⚡ Quick Reference

GoalLegacy (.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 menuevent.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()

📋 .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

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

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.

jQuery
$( ".item" ).contextmenu( { menu: "actions" }, function( event ) {
  var label = $( this ).data( "label" );
  $( "#out" ).text(
    "Right-clicked: " + label + " | eventData.menu: " + event.data.menu
  );
} );
Try It Yourself

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.

jQuery
var count = 0;

$( "#target" ).contextmenu( function() {
  count++;
  $( "#out" ).text( "Target handler fired (count: " + count + ")" );
} );

$( "#fire" ).click( function() {
  $( "#target" ).contextmenu();
} );
Try It Yourself

How It Works

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

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

jQuery
$( "#panel" )
  .contextmenu( function( event ) {
    event.preventDefault();
    $( "#out" ).text( "Right-clicked — native menu blocked via preventDefault()." );
  } )
  .addClass( "has-menu" );
Try It Yourself

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.

🚀 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.
  • Proxy triggers$("#shortcut").click(function(){ $("#target").contextmenu(); }) — replace inner no-arg .contextmenu() with .trigger("contextmenu").
  • Shared eventData$(".row").contextmenu({ type: "grid" }, fn) passes metadata without closures — migrate to .on("contextmenu", { type: "grid" }, fn).
  • 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").

📝 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.
  • For the full modern contextmenu guide — custom menus, coordinates, and keyboard key — see the jQuery contextmenu event tutorial.

Browser Support

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 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 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.

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.

💡 Best Practices

✅ Do

  • Use .on("contextmenu", handler) for all new right-click bindings
  • Use .trigger("contextmenu") instead of no-arg .contextmenu()
  • Call event.preventDefault() when showing a custom context menu
  • Migrate .contextmenu(fn) to .on("contextmenu", fn) when refactoring legacy files
  • Read the modern contextmenu event tutorial for full patterns

❌ Don’t

  • 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

Key Takeaways

Knowledge Unlocked

Six things to remember about .contextmenu()

Legacy bind, modern migrate.

6
Core concepts
data 02

eventData

Since 1.4.3

Handler
() 03

.contextmenu()

Trigger

No args
.on 04

.on("contextmenu")

Modern

Replace
05

.trigger()

Fire

Programmatic
3.3 06

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.

Next: .hover() Method

Learn mouseenter/mouseleave shorthand — and how to migrate to .on().

.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.

6 people found this page helpful