HTML | oncontextmenu Event Attribute
This attribute works when the user right-clicks on an element to open the context menu.
Syntax:
<element oncontextmenu="script">
Attribute Value: This attribute contains single value script. The script to be run when oncontextmenu attribute called. It is supported by all HTML elements.
Note: This attribute is new in HTML 5.
Example:
<!DOCTYPE html> <html> <head> <title>oncontextmenu Event Attribute</title> <style> div { background: Green; border: 1px solid black; padding: 10px; width:60%; color:white; } h1 { color:green; } </style> </head> <body> <center> <h1>GeeksforGeeks</h1> <h2>oncontextmenu Event Attribute</h2> <div oncontextmenu="myFunction()" contextmenu="mymenu"> <p>Right-click inside this box</p> </div> <script> function myFunction() { alert("You right-clicked inside the div!"); } </script> </center> </body> </html> |
Output:

Supported Browsers: The browser supported by oncontextmenu attribute are listed below:
- Chrome
- Internet Explorer
- Firefox
- Safari
- Opera
Recommended Posts:
- HTML | DOM oncontextmenu Event
- HTML | onwheel Event Attribute
- HTML | onpaste Event Attribute
- HTML | onselect Event Attribute
- HTML | onoffline Event Attribute
- HTML | onfocus Event Attribute
- HTML | ondblclick Event Attribute
- HTML | onload Event Attribute
- HTML | onmousemove Event Attribute
- HTML | onmouseup Event Attribute
- HTML | onreset Event Attribute
- HTML | onresize Event Attribute
- HTML | ondrag Event Attribute
- HTML | ondragend Event Attribute
- HTML | ontoggle Event Attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh



