HTML event attribute oncontextmenu

The oncontextmenu attribute event is triggered when opening the context menu.

We can normally open the context menu by right-clicking on an element.

What's new in HTML5

The oncontextmenu attribute is new in HTML5.


<element oncontextmenu="script or Javascript function name">

Supported Tags

All HTML elements.

Browser compatibility

oncontextmenu Yes Yes Yes Yes Yes


<!DOCTYPE html>
div {<!--from  ww w .  java 2  m-->
    border: 1px solid black;
    padding: 10px;
    <div oncontextmenu="myFunction()" contextmenu="mymenu">
        Right-click inside this box to see the context menu!
    function myFunction() {
        alert("right-clicked inside the div!");

Click to view the demo