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