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
Fundamentals
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.
Concept
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.
Foundation
📝 Syntax
The jQuery API for the contextmenu event has two main forms — binding a handler and triggering the event:
Fires on press — check event.which (3 = right button)
preventDefault
block menu
Required to replace the native context menu with your own UI
Hands-On
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.
User right-clicks #target → alert: "Handler for contextmenu called."
Browser menu may still appear unless preventDefault() is called
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.
Right-click any paragraph → alert: "Hello World!"
Each p element has its own handler via the shared selector
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" );
} );
Right-click the blue block → toggles .contextmenu class (yellow background)
Right-click again → toggles back to blue
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.
Right-click #panel → native menu blocked, custom menu at cursor
Left-click anywhere → custom menu hides
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.
.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.
Applications
🚀 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 menu — event.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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
contextmenuUniversal
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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about the contextmenu event
Right-click, prevent, position.
6
Core concepts
.on01
.on("contextmenu")
Bind
API
🖱02
Right-click
Secondary btn
Gesture
🚫03
preventDefault
Block menu
Custom UI
XY04
pageX / pageY
Position
Coords
⚡05
.trigger()
Fire
Programmatic
click06
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.