oncontextmenu = myFunction(); - Javascript DOM Event

Javascript examples for DOM Event:Element Event Attribute

Description

The oncontextmenu event occurs when the user right-clicks on an element.

Summary

Bubbles Yes
Cancelable Yes
Supported HTML tags: All HTML elements

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {//from  ww  w. j a va  2s.com
    background: yellow;
    border: 1px solid black;
    padding: 10px;
}
</style>
</head>
<body>

<p>This example uses the HTML DOM to assign an "oncontextmenu" event to a div element.</p>

<div id="myDIV" contextmenu="mymenu">
<p>Right-click inside this box to see the context menu!

<menu type="context" id="mymenu">
  <menuitem label="Q" onclick="console.log('Q')" icon=""></menuitem>
  <menu label="Share on...">
    <menuitem label="A" icon="" onclick="console.log('A')"></menuitem>
    <menuitem label="B" icon="" onclick="console.log('B')"></menuitem>
  </menu>
  <menuitem label="E" onclick="console.log('E')"></menuitem>
</menu>

</div>

<p id="demo"></p>

<script>
document.getElementById("myDIV").oncontextmenu = function() {myFunction()};

function myFunction() {
    var x = document.getElementById("demo");
    x.innerHTML = "You right-clicked inside div!";
    x.style.fontSize = "30px";
}
</script>

</body>
</html>

Related Tutorials