HTML - attributes - oncontextmenu

revision:


Content

"oncontextmenu" attribute: open context menu on right-click. syntax some examples


"oncontextmenu" attribute: open context menu on right-click.

top

This attribute specifies through the user right-clicks on an element to open the context menu.It supports all HTML elements.


syntax

top

<element oncontextmenu="script></element>

script: the script to be run on oncontextmenu.


some examples

top
codes:
                    <div style="margin-left:5vw;" id="menu" oncontextmenu="clickRight()" contextmenu="mymenu">
                        <p class="spec">Right-click inside this box</p>
                    </div>
                    <script>
                        function clickRight() {
                            alert("You right-clicked inside the div!");
                        }
                    </script>
                
Hi there !!

Now right click on above red box to open context menu.

codes:
                    <div style="margin-left:3vw;color:white;" oncontextmenu="get()" id="box">Hi there !!</div>
                    <p class="spec">Now try to right click on above red box to open context menu.</p>
                    <script>
                        function get() {
                        document.getElementById("box").style.background = "#084C61";
                        document.getElementById("box").style.color = "darkblue";
                        alert("you right-clicked the box");
                        }
                    </script>