jQuery Contextmenu Event

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

What You’ll Learn

The contextmenu event fires when the user right-clicks an element — before the browser shows its default menu. This tutorial covers binding with .on("contextmenu"), blocking the native menu with event.preventDefault(), building custom menus, triggering with .trigger("contextmenu"), and how it differs from the click event.

01

.on()

Bind handler

02

Right-click

Secondary button

03

preventDefault

Block native menu

04

pageX / pageY

Menu position

05

.trigger()

Fire event

06

Since 1.0

Stable API

Introduction

Right-clicking a web page normally opens the browser’s context menu — Copy, Inspect, Back, and other options. Many apps replace that menu with their own: “Rename,” “Delete,” or “Open in new tab.” jQuery’s contextmenu event is the hook for that behavior.

The event is sent to an element when the right mouse button is clicked on it, but before the context menu is displayed. If the user presses the context-menu keyboard key instead, the event fires on the html element or the currently focused element. Any HTML element can receive this event — not just images or links.

Understanding the contextmenu Event

Unlike click, which responds to a left-button press-and-release, contextmenu specifically handles the secondary (usually right) mouse button. jQuery passes a normalized event object with coordinates (event.pageX, event.pageY), the target element (event.target), and methods like event.preventDefault().

Your handler runs first. If you call event.preventDefault(), the browser skips its default menu — leaving room for a custom HTML menu positioned at the cursor. If you omit preventDefault(), both your handler and the native menu run (handler first, then the browser menu).

💡
Beginner Tip

Left-click handlers (.on("click")) do not run on right-click. Always bind contextmenu separately when you need right-click behavior. For accessibility, remember the context-menu keyboard key triggers the same event.

📝 Syntax

The jQuery API for the contextmenu event has two main forms — binding a handler and triggering the event:

1. Bind a handler — .on("contextmenu" [, eventData ], handler) (since 1.0)

jQuery
.on( "contextmenu" [, eventData ], handler )
  • eventData — optional object passed to the handler as event.data (since 1.4.3).
  • handler — function called each time contextmenu fires: function( event ) { ... }.

2. Block the browser menu — event.preventDefault()

jQuery
$( "#target" ).on( "contextmenu", function( event ) {
  event.preventDefault();
  // show your custom menu at event.pageX, event.pageY
} );

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

jQuery
.trigger( "contextmenu" )
  • Runs bound contextmenu handlers programmatically.
  • Use .triggerHandler("contextmenu") to run handlers without bubbling side effects.

4. Unbind — .off("contextmenu" [, handler])

jQuery
$( "#target" ).off( "contextmenu" );   // remove all contextmenu handlers

Official jQuery API example

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

Return value

  • .on() and .trigger() return the original jQuery object for chaining.
  • Returning false from a handler is equivalent to preventDefault() + stopPropagation().

⚡ Quick Reference

GoalCode
Bind right-click handler$("#box").on("contextmenu", fn)
Block browser menuevent.preventDefault()
Get cursor positionevent.pageX, event.pageY
Pass data to handler$("#box").on("contextmenu", { id: 1 }, fn)
Trigger programmatically$("#target").trigger("contextmenu")
Remove handlers$("#box").off("contextmenu")
One-time handler$("#box").one("contextmenu", fn)

📋 contextmenu vs click vs mousedown

Three mouse-related events — pick the one that matches the user gesture you need.

contextmenu
right-click

Secondary button before browser menu — custom menus, inspect actions

click
left click

Primary button press-and-release — buttons, links, toggles

mousedown
any button

Fires on press — check event.which (3 = right button)

preventDefault
block menu

Required to replace the native context menu with your own UI

Examples Gallery

Examples 1–3 follow the official jQuery API documentation. Examples 4–5 cover preventDefault() and programmatic triggers. Use the Try-it links to run each snippet — right-click inside the demo area to test.

📚 Binding Handlers

Official jQuery demos for contextmenu handlers.

Example 1 — Official Demo: Bind Handler on #target

Alert when the user right-clicks the target element — the canonical .on("contextmenu") pattern.

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

How It Works

.on("contextmenu", fn) registers the function on the element. jQuery calls it when the browser fires a right-click — before the native menu opens. Without preventDefault(), the browser menu still appears after your handler.

Example 2 — Official Demo: “Hello World!” on Paragraphs

Show an alert when the user right-clicks any paragraph on the page.

jQuery
$( "p" ).on( "contextmenu", function() {
  alert( "Hello World!" );
} );
Try It Yourself

How It Works

Binding to all p elements attaches one handler per paragraph. Right-clicking any of them runs the alert. This is the simplest way to test that contextmenu is wired correctly.

Example 3 — Official Demo: Right-Click to Toggle Background Color

Toggle a highlight class when the user right-clicks a block — no alert, just visual feedback.

jQuery
var div = $( "div" ).first();

div.on( "contextmenu", function() {
  div.toggleClass( "contextmenu" );
} );
Try It Yourself

How It Works

.toggleClass("contextmenu") adds or removes the class on each right-click. CSS switches from blue to yellow. This mirrors the official jQuery API demo and shows a practical non-alert use case.

📈 Custom Menus & Triggers

Block the native menu and fire handlers programmatically.

Example 4 — Custom Context Menu with preventDefault()

Block the browser menu and show a simple custom menu at the cursor position.

jQuery
var $menu = $( "#custom-menu" );

$( "#panel" ).on( "contextmenu", function( event ) {
  event.preventDefault();
  $menu.css( { left: event.pageX, top: event.pageY } ).show();
} );

$( document ).on( "click", function() {
  $menu.hide();
} );
Try It Yourself

How It Works

event.preventDefault() stops the browser’s default context menu. event.pageX and event.pageY place your menu at the pointer. A document-level click handler hides the menu when the user clicks elsewhere — a pattern every custom menu needs.

Example 5 — Trigger contextmenu Programmatically

Fire the contextmenu handler from a button — useful for testing or keyboard shortcuts.

jQuery
$( "#target" ).on( "contextmenu", function() {
  $( "#log" ).text( "contextmenu handler ran." );
} );

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

How It Works

.trigger("contextmenu") synthetically fires the event on #target, running the same handler as a physical right-click. Useful for unit tests, automation, or proxy buttons that open the same menu logic.

🚀 Common Use Cases

  • Custom context menus — file browsers, kanban boards, and image galleries with “Rename,” “Delete,” or “Download.”
  • Canvas and diagram editors — right-click a shape to open editing options at the cursor.
  • Table row actions — right-click a row for “Edit,” “Duplicate,” or “Archive” without cluttering every row with buttons.
  • Disable default menuevent.preventDefault() on games or kiosk UIs where browser menus are unwanted.
  • Inspect coordinates — log event.pageX / event.pageY for debugging layout or map tools.
  • EventData labels.on("contextmenu", { action: "copy" }, fn) to pass metadata without closures.

🧠 How the contextmenu Event Flows

1

Right-click (or menu key)

User presses the secondary mouse button on an element, or the context-menu key on the focused element.

Input
2

Handler runs

jQuery calls your .on("contextmenu") handler with the event object — before the browser menu appears.

Handler
3

preventDefault?

If called, the native menu is skipped. Otherwise the browser shows its default Copy / Inspect menu.

Branch
4

Custom menu (optional)

Position your HTML menu at pageX / pageY and hide it on outside click or Escape.

📝 Notes

  • Bind with .on("contextmenu", handler) since jQuery 1.0 — not the deprecated .contextmenu(handler) shorthand.
  • Trigger with .trigger("contextmenu") since jQuery 1.0.
  • The event fires before the browser menu — call preventDefault() to replace it.
  • Left-click (click) and right-click (contextmenu) are separate — bind both if needed.
  • Context-menu keyboard key fires on html or the focused element — test keyboard accessibility.
  • On macOS, Ctrl+click also triggers contextmenu — same as right-click on a two-button mouse.
  • Custom menus should close on Escape and outside click; consider role="menu" for screen readers.

Browser Support

The contextmenu event is a standard DOM event supported in every browser jQuery targets. jQuery’s .on("contextmenu") and .trigger("contextmenu") (both since 1.0) normalize cross-browser behavior.

jQuery 1.0+

jQuery contextmenu event

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery versions. Native equivalent: element.addEventListener('contextmenu', fn). Use preventDefault() to block the native menu in all major browsers.

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 Universal

Bottom line: Safe in any jQuery project. Always call preventDefault() when showing a custom menu. Test Ctrl+click on macOS and the context-menu keyboard key for accessibility.

Conclusion

The jQuery contextmenu event is how you respond to right-clicks before the browser shows its default menu. Bind handlers with .on("contextmenu", fn), block the native menu with event.preventDefault(), and position custom UI with event.pageX and event.pageY.

Remember: click handles left-button interactions; contextmenu handles right-click and the context-menu key. For production custom menus, hide on outside click and Escape, and consider keyboard-accessible alternatives alongside right-click.

💡 Best Practices

✅ Do

  • Call event.preventDefault() when showing a custom menu
  • Bind with .on("contextmenu", handler) — the modern API
  • Hide custom menus on document click and Escape key
  • Use event.pageX / event.pageY for cursor positioning
  • Provide keyboard-accessible alternatives to right-click actions

❌ Don’t

  • Assume click handlers cover right-clicks — they do not
  • Use deprecated .contextmenu(handler) in new code
  • Block the native menu without offering equivalent actions elsewhere
  • Forget macOS Ctrl+click — it also fires contextmenu
  • Leave custom menus open after the user clicks outside

Key Takeaways

Knowledge Unlocked

Six things to remember about the contextmenu event

Right-click, prevent, position.

6
Core concepts
🖱 02

Right-click

Secondary btn

Gesture
🚫 03

preventDefault

Block menu

Custom UI
XY 04

pageX / pageY

Position

Coords
05

.trigger()

Fire

Programmatic
click 06

Not click

Separate event

Pitfall

❓ Frequently Asked Questions

The contextmenu event fires when the user right-clicks an element (or presses the context-menu keyboard key on the focused element) — before the browser's default menu appears. Bind handlers with .on('contextmenu', handler) since jQuery 1.0. Any HTML element can receive this event.
Call event.preventDefault() inside your handler. Without it, the browser shows its native context menu after your handler runs. For custom menus, also position your menu with event.pageX and event.pageY, then hide it on document click or Escape.
Use .on('contextmenu', handler) for binding — the modern API since jQuery 1.7. The old .contextmenu(handler) shorthand is deprecated like .click(handler). For triggering, prefer .trigger('contextmenu') over no-arg .contextmenu().
click fires on a full left-button press-and-release inside an element. contextmenu fires on a right-button click (or context-menu key) before the browser menu opens. They are separate events — binding click does not handle right-clicks.
Call .trigger('contextmenu') on the target: $('#target').trigger('contextmenu'). Available since jQuery 1.0, this runs bound handlers. Note that synthetic triggers may not reproduce every browser default-menu behavior — test in your target browsers.
Yes. When the user presses the context-menu key (often near the right Ctrl key), the browser fires contextmenu on the html element or the currently focused element. Your .on('contextmenu') handler runs the same way as for a mouse right-click.
Did you know?

The jQuery API page for contextmenu notes that pressing the context-menu keyboard key triggers the event on the html element or the currently focused element — not necessarily where the mouse pointer is. When building custom menus, check whether the event came from keyboard or mouse and position accordingly.

Next: .contextmenu() Method

Learn the deprecated shorthand — and how to migrate to .on("contextmenu").

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