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.

Syntax

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

Supported Tags

All HTML elements.

Browser compatibility

oncontextmenu Yes Yes Yes Yes Yes




Example

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

Click to view the demo