addEventListener("contextmenu", myFunction); - Javascript DOM Event

Javascript examples for DOM Event:addEventListener

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 {/*w  w  w.  j  a v a2 s .co  m*/
    background: yellow;
    border: 1px solid black;
    padding: 10px;
}
</style>
</head>
<body>

<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").addEventListener("contextmenu", myFunction);

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

</body>
</html>

Related Tutorials