jQuery .contextmenu() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Mouse Events

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("#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")
CallWhat it does
.contextmenu()Learn the deprecated jQuery .contextmenu() shorthand: bind with .contextmenu(handler), pass eventData, trigger with no-arg .contextmenu(), and migrate to .on(\

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select element(s): $("#panel").
Step 2Bind (legacy): .contextmenu(function (event) { ... }).
Step 3Optional data (1.4.3+): .contextmenu({ key: "v" }, handler) → event.data.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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 — Deprecated Enter/Leave Shorthand

Bind mouseenter/mouseleave with one helper.

.hover() Method — Deprecated Enter/Leave Shorthand tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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