jQuery Contextmenu Event

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

Definition

contextmenu is sent when the user right-clicks an element — before the browser context menu appears — or when the context-menu key is pressed. Bind with .on("contextmenu", handler).

Syntax

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

jQuery
$("#target").on("contextmenu", function (event) { ... })
$("#target").trigger("contextmenu")

// Fires before the native menu
// Use event.preventDefault() to block that menu
// Prefer on/trigger — not deprecated .contextmenu()
CallWhat it does
ContextmenuLearn the jQuery contextmenu event: bind right-click with .on(\

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

Parameters

ParameterDescription
Step 1Select the element(s): $("#panel"), $("p"), etc.
Step 2Bind: .on("contextmenu", function (event) { ... }).
Step 3Optional: event.preventDefault() to hide the browser menu.

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 a Contextmenu Handler

Official style: .on("contextmenu", …) on a target.

jQuery
$("#target").on("contextmenu", function () {
  console.log("Handler for contextmenu called.");
});
Try It Yourself

How It Works

  1. Select #target.
  2. Bind with .on("contextmenu", handler).
  3. User right-clicks the element.

Example 2 — Block the Browser Menu

Call preventDefault() so the native menu does not open.

jQuery
$("#target").on("contextmenu", function (event) {
  event.preventDefault();
  console.log("Custom action — native menu blocked.");
});
Try It Yourself

How It Works

  1. Bind contextmenu on #target.
  2. preventDefault() cancels the default menu.
  3. Your code can show a custom menu or message instead.

Example 3 — Trigger Contextmenu from Code

Fire the same handlers without a real right-click.

jQuery
$("#target").on("contextmenu", function () {
  console.log("Handler for contextmenu called.");
});

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

How It Works

  1. Bind the real handler on #target.
  2. #fire listens for a normal click.
  3. trigger("contextmenu") runs #target’s handlers.

Frequently Asked Questions

contextmenu fires when the user right-clicks an element (before the browser menu appears), or when the context-menu key is pressed on the focused element. Bind with .on("contextmenu", handler).
Call event.preventDefault() in your handler. Without it, the native menu still opens after your code runs.
Prefer .on("contextmenu", handler). The .contextmenu() shorthand is deprecated since jQuery 3.3. Trigger with .trigger("contextmenu").
click is the left-button press-and-release sequence. contextmenu is a separate right-click (or menu-key) event. Binding click does not handle right-clicks.
Use $(selector).trigger("contextmenu"). That runs bound handlers. Synthetic triggers may not always show the native browser menu — test in your browsers.
Yes. The browser fires contextmenu on the html element or the focused element. Your .on("contextmenu") handler runs the same way as for a mouse right-click.

Next: .contextmenu() Method

See the deprecated helper and how it maps to on / trigger.

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

9 people found this page helpful