JavaScript Element contextmenu Event

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM event
Instance event

What You’ll Learn

The contextmenu event fires when the user tries to open a context menu—usually with a right-click or the context menu key. Learn preventDefault for custom menus, clientX / clientY, addEventListener vs oncontextmenu, Firefox Shift quirks, and five try-it labs.

01

Kind

Instance event

02

Type

PointerEvent

03

When

Right-click / menu key

04

Handler

oncontextmenu

05

Cancel

preventDefault()

06

Status

Limited availability

Introduction

Browsers show a built-in context menu when you right-click (copy, inspect, open link, and so on). The contextmenu event is your chance to react—log the action, block the default menu, or show a custom menu for your app.

Any right-click that was not disabled earlier (for example by canceling the related click path) typically leads to a contextmenu event on the target.

💡
Beginner tip

Call event.preventDefault() to hide the browser menu, then place your own menu near event.clientX and event.clientY. Always offer another way to open the same actions on touch devices.

Understanding contextmenu

An instance event that answers: “Is the user trying to open a context menu on this element?”

  • Triggers — right mouse button, context menu key (and similar).
  • PointerEvent (inherits MouseEvent) in modern specs.
  • CancelablepreventDefault() can stop the browser menu.
  • Useful fieldsclientX, clientY, target.
  • Limited availability on MDN (not Baseline)—test mobile carefully.

📝 Syntax

Use the event name with addEventListener, or set the handler property:

JavaScript
addEventListener("contextmenu", (event) => { });

oncontextmenu = (event) => { };

Event type

A PointerEvent (inherits from MouseEvent and Event).

Handy inherited properties

PropertyMeaning
clientX / clientYPointer position in the viewport (great for placing a custom menu)
buttonMouse button (right-click is typically 2)
targetElement that received the event

⚠️ Firefox Shift + Right-Click

MDN notes a Firefox exception: if the user holds Shift while right-clicking, Firefox may show the context menu without firing contextmenu. In that case preventDefault() cannot hide the menu because your handler never runs.

Treat custom menus as progressive enhancement, not a security lock. Users can still use other browser chrome or OS features.

⚖️ contextmenu vs click / auxclick

EventTypical trigger
clickPrimary button activation
auxclickNon-primary buttons (often middle / sometimes right)
contextmenuAttempt to open a context menu (right-click / menu key)

⚡ Quick Reference

GoalCode / note
Listenel.addEventListener("contextmenu", fn)
Handler propertyel.oncontextmenu = fn
Disable browser menuevent.preventDefault()
Place custom menuevent.clientX, event.clientY
MDN statusLimited availability (not Baseline)

🔍 At a Glance

Four facts to remember about contextmenu.

Event type
PointerEvent

MouseEvent fields too

Means
open menu?

Right-click / key

Custom UI
preventDefault

Then show your menu

Baseline
no

Limited availability

Examples Gallery

Examples follow MDN Element: contextmenu event. Right-click the demo targets (or use the context menu key where available).

📚 Getting Started

Cancel the browser menu and listen with the handler property.

Example 1 — Cancel with preventDefault() (MDN)

MDN idea: disable the context menu on one paragraph only.

JavaScript
const noContext = document.getElementById("noContextMenu");

noContext.addEventListener("contextmenu", (e) => {
  e.preventDefault();
});
Try It Yourself

How It Works

Only the listened element is affected. Neighbors keep the default browser menu unless you also cancel there.

Example 2 — oncontextmenu Property

Same cancel pattern using the handler property.

JavaScript
const box = document.getElementById("box");

box.oncontextmenu = (event) => {
  event.preventDefault();
  console.log("context menu blocked");
};
Try It Yourself

How It Works

Prefer addEventListener when you need multiple listeners or easy removal.

📈 Coordinates, Custom Menu & Compare

Log pointer position, show a tiny custom menu, and compare two paragraphs.

Example 3 — Log clientX / clientY

See where the user opened the menu—useful before building custom UI.

JavaScript
const area = document.getElementById("area");
const out = document.getElementById("out");

area.addEventListener("contextmenu", (event) => {
  event.preventDefault();
  out.textContent = "x=" + event.clientX + ", y=" + event.clientY;
});
Try It Yourself

How It Works

Coordinates are in the viewport. Convert or clamp them if your menu might overflow the window edges.

Example 4 — Tiny Custom Menu

Cancel the default menu and show a simple absolute-positioned panel.

JavaScript
const surface = document.getElementById("surface");
const menu = document.getElementById("menu");

surface.addEventListener("contextmenu", (event) => {
  event.preventDefault();
  menu.style.display = "block";
  menu.style.left = event.clientX + "px";
  menu.style.top = event.clientY + "px";
});

document.addEventListener("click", () => {
  menu.style.display = "none";
});
Try It Yourself

How It Works

This is a teaching sketch. Production menus need keyboard focus, Escape to close, and accessibility roles.

Example 5 — MDN Two-Paragraph Demo

Full MDN HTML+JS pattern: one paragraph blocks the menu; the other does not.

JavaScript
<p id="noContextMenu">The context menu has been disabled on this paragraph.</p>
<p>But it has not been disabled on this one.</p>

<script>
const noContext = document.getElementById("noContextMenu");
noContext.addEventListener("contextmenu", (e) => {
  e.preventDefault();
});
</script>
Try It Yourself

How It Works

Scope your listener tightly. Blocking context menus everywhere can frustrate users and accessibility tools.

🚀 Common Use Cases

  • Building custom right-click menus for editors, tables, or maps.
  • Disabling the browser menu on a canvas or game surface.
  • Logging where users open menus for UX research.
  • Showing contextual actions for a selected row or file.
  • Pairing with a visible “More” button for touch / iOS users.

🔧 How It Works

1

User requests a menu

Right-click or press the context menu key on a focused control.

Input
2

contextmenu fires

Your listener runs on the target (and can bubble to parents).

Event
3

Default or custom

Allow the browser menu, or preventDefault() and show your own UI.

Choice
4

Actions appear

The user picks a command from the browser menu or your custom menu.

📝 Notes

  • MDN: Limited availability (not Baseline)—desktop is usually fine; iOS Safari often is not.
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Firefox Shift+right-click may bypass the event entirely.
  • Do not treat preventDefault() as a way to protect content from copying.
  • Related learning: click, copy, addEventListener(), JavaScript hub.

Limited Browser Availability

contextmenu is marked Limited availability on MDN (not Baseline). Logos use the shared browser-image-sprite.png sprite from this project. Desktop browsers generally support it; iOS Safari typically does not fire the event—ship an alternate menu control for touch.

Limited availability

Element contextmenu

Strong on desktop right-click / menu-key paths; incomplete on some mobile browsers such as iOS Safari.

Limited Not Baseline
Google Chrome Supported · Desktop & Android (verify touch UX)
Supported
Mozilla Firefox Supported · Shift+right-click quirk
Supported
Apple Safari Desktop supported; iOS typically no event
Partial
Microsoft Edge Supported · Chromium
Supported
Opera Supported · Modern versions
Supported
Internet Explorer Legacy support for basic contextmenu
Legacy
contextmenu Limited

Bottom line: Use contextmenu for custom desktop menus with preventDefault; always provide a visible menu button for mobile users.

Conclusion

contextmenu is the “user wants a context menu” signal. Listen with addEventListener, optionally call preventDefault(), and place a custom menu with clientX / clientY—while remembering Limited availability on some mobile browsers.

Continue with copy, click, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Prefer addEventListener("contextmenu", ...)
  • Call preventDefault() only when you show a real custom menu
  • Position menus with clientX / clientY
  • Provide a visible alternate control for touch / iOS
  • Close the custom menu on outside click or Escape

❌ Don’t

  • Disable context menus site-wide without a good reason
  • Assume iOS Safari will fire contextmenu
  • Treat menu blocking as content protection
  • Forget Firefox Shift+right-click can bypass your handler
  • Overwrite oncontextmenu if you need multiple listeners

Key Takeaways

Knowledge Unlocked

Five things to remember about contextmenu

Right-click signal—cancel default, show custom UI, support mobile another way.

5
Core concepts
📄 02

PointerEvent

clientX / clientY

API
🚫 03

preventDefault

hide browser menu

Pattern
📱 04

Mobile gap

offer a button

UX
🎯 05

Limited

not Baseline

Compat

❓ Frequently Asked Questions

It fires when the user attempts to open a context menu — typically by right-clicking or pressing the context menu key on the keyboard.
No. MDN does not mark it Deprecated, Experimental, or Non-standard. It has Limited availability (not Baseline), mainly because support is incomplete on some platforms such as iOS Safari.
A PointerEvent (inherits from MouseEvent). Older specs used MouseEvent; check compatibility if you rely on PointerEvent-only fields.
Call event.preventDefault() in your contextmenu handler, then show your own UI — often positioned with event.clientX and event.clientY.
In Firefox, holding Shift while right-clicking shows the context menu without firing the contextmenu event, so canceling the event cannot block that path.
Desktop browsers generally fire contextmenu on right-click. On many touch devices (notably iOS Safari) the event may not fire; provide an alternate control such as a menu button for mobile users.
Did you know?

When the context menu key (not the mouse) opens a menu, browsers often place it near the bottom-left of the focused element—or the current row in a tree control—rather than at a pointer coordinate.

Next: copy

Customize what goes on the clipboard when users copy.

copy →

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.

5 people found this page helpful