👀 Live Preview
Right-click the box below (browser menu is blocked for this demo):
Try a right-click on the box.

The oncontextmenu attribute is an inline event handler that runs JavaScript when the contextmenu event fires. That usually happens when the user right-clicks an element (or long-presses on touch devices) — the gesture that opens a browser context menu. Developers use it to run custom logic, show a custom menu, or call event.preventDefault() to block the default menu. The contextmenu event bubbles, so you can listen on a parent. For production sites, prefer addEventListener("contextmenu", …) and always consider accessibility when replacing the browser menu.
Inline JS.
Right-click.
Block default.
Right vs left.
Preferred way.
Build your own.
oncontextmenu AttributeThe primary purpose of oncontextmenu is to react when the user opens a context menu on an element — typically by right-clicking. Common uses include showing a custom menu with actions like “Copy link” or “Delete item”, disabling the default menu on images, or running validation before allowing browser actions. Pair it with event.preventDefault() when you replace the built-in menu with your own UI.
Do not confuse oncontextmenu with onclick. A right-click may also fire other events, but contextmenu is specifically tied to the menu gesture. Blocking the default menu without offering alternatives hurts usability — only customize when it clearly helps the user.
To hide the browser’s context menu, call event.preventDefault() inside your handler, then display your custom menu at event.clientX and event.clientY.
Set oncontextmenu on any element or assign element.oncontextmenu:
<script>
function handleContextMenu(event) {
event.preventDefault();
document.getElementById("status").textContent = "Custom menu action!";
}
</script>
<div id="box" oncontextmenu="handleContextMenu(event)">
Right-click me
</div>
<p id="status"></p>event to the handler so you can call event.preventDefault().div, img, button, p, etc.element.oncontextmenu = function(e) { … }.element.addEventListener("contextmenu", handler).contextmenu event bubbles — you can delegate on a parent container.The oncontextmenu attribute accepts a string of JavaScript code:
oncontextmenu="handleMenu(event)" — Call a function with the event object.oncontextmenu="event.preventDefault()" — Block the browser menu (use carefully).box.oncontextmenu = (e) => { … } assigns the handler.const box = document.getElementById("box");
box.addEventListener("contextmenu", (event) => {
event.preventDefault();
showCustomMenu(event.clientX, event.clientY);
});
// Dynamic assignment
document.getElementById("photo").oncontextmenu = function (e) {
e.preventDefault();
console.log("Image context menu blocked");
};| Event | When it fires | Handler |
|---|---|---|
contextmenu | Right-click / long-press | oncontextmenu |
click | Primary click | onclick |
preventDefault() | Block browser menu | In handler |
clientX / clientY | Pointer position | Position custom menu |
| Touch devices | Long-press gesture | Also fires contextmenu |
| Bubbles? | contextmenu yes | Delegate on parent |
| Target | Supported? | Notes |
|---|---|---|
<div>, <span> | Yes | Common for custom menu zones |
<img> | Yes | Often used to protect or customize image menus |
<button> | Yes | Right-click on buttons |
<a href> | Yes | Custom link actions on right-click |
<input>, <textarea> | Yes | Browser may still show text-editing menu items |
oncontextmenu vs onclick vs onmousedown| Handler | When it fires | Typical use |
|---|---|---|
onmousedown | Any mouse button pressed | Drag, press feedback |
onclick | Primary click (left button) | Buttons, toggles, links |
oncontextmenu | Right-click / menu gesture | Custom context menus |
Inline oncontextmenu with preventDefault, addEventListener on an image, and a simple custom context menu.
Right-click the box below (browser menu is blocked for this demo):
Try a right-click on the box.
Run a function when the user right-clicks, and block the default menu:
<button type="button" oncontextmenu="handleMenu(event)">
Right-click me
</button>
<script>
function handleMenu(event) {
event.preventDefault();
document.getElementById("msg").textContent = "Custom context action!";
}
</script>The browser fires contextmenu before showing the default menu. Your handler can cancel it and run custom logic instead.
Attach the handler with addEventListener on an image:
<script>
document.getElementById("photo").addEventListener("contextmenu", function (event) {
event.preventDefault();
showCustomMenu(event.clientX, event.clientY);
});
// Or property form:
document.getElementById("photo").oncontextmenu = function (e) { e.preventDefault(); };
</script>Register the listener once at page load. On right-click, prevent the default menu and show your UI at the pointer coordinates.
Build a minimal menu div that appears on right-click:
area.addEventListener("contextmenu", (e) => {
e.preventDefault();
menu.style.left = e.clientX + "px";
menu.style.top = e.clientY + "px";
menu.hidden = false;
});
document.addEventListener("click", () => { menu.hidden = true; });preventDefault() blocks the browser menu. Position your menu with CSS left / top from the event coordinates.
preventDefault(), provide a custom menu with the same essential actions (copy, open link, etc.).role="menu", role="menuitem", and visible focus on custom menu items.Or long-press on touch.
Before default menu shows.
Optional preventDefault().
Show UI at cursor.
The contextmenu event and oncontextmenu handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge. Long-press on mobile also triggers the event on supported elements.
oncontextmenu supportAll major browsers fire the underlying event and honor the oncontextmenu handler attribute.
Bottom line: Universal support. Test custom menus on desktop (right-click) and mobile (long-press) before shipping.
oncontextmenu.The oncontextmenu attribute runs JavaScript when a user opens a context menu on an element and the contextmenu event fires. Use it with event.preventDefault() to show custom menus or run targeted actions on right-click.
Prefer addEventListener("contextmenu", …), provide accessible alternatives when blocking the default menu, and test on both desktop and touch devices.
oncontextmenuBookmark these before wiring your next event handler.
contextmenu fires.
Eventdiv, img, button.
ScopeBlock default menu.
PatternPreferred.
ModernAt clientX/Y.
Notecontextmenu event fires — usually on right-click or long-press.div, img, button, p, and more.event.preventDefault(), then show your menu at event.clientX and event.clientY.onclick is a primary click. oncontextmenu is the right-click / menu gesture.element.addEventListener("contextmenu", …). Inline oncontextmenu works for simple demos.Practice inline oncontextmenu, preventDefault, and custom menus in the Try It editor.
5 people found this page helpful