Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Create a command/menu item that the user can invoke from a popup menu in HTML5
The <menuitem> tag in HTML5 was designed to create command/menu items that users can invoke from popup context menus. However, it's important to note that this element has been deprecated and is no longer supported in modern browsers due to lack of widespread adoption and implementation inconsistencies.
The <menuitem> tag was intended to work with the <menu> element to create interactive context menus that would appear when users right-clicked on elements with a contextmenu attribute.
Syntax
Following is the syntax for the deprecated <menuitem> tag −
<menu type="context" id="menuId"> <menuitem label="Menu Item" type="command" onclick="function()"></menuitem> </menu>
Attributes
The <menuitem> tag supported the following attributes −
| Attribute | Value | Description |
|---|---|---|
| checked | checked | Defines that a menuitem should be checked |
| default | default | Marks a menuitem as a default command |
| disabled | disabled | Disables a menuitem so it cannot be clicked |
| icon | URL | Defines an icon for a menuitem |
| label | text | Defines a name for a menuitem displayed to the user |
| radiogroup | groupname | Defines a group of commands where only one can be selected |
| type | checkbox, command, radio | Defines type of command for a menuitem (default is command) |
Example − Deprecated Menuitem Tag
Following example shows how the <menuitem> tag was intended to work −
<!DOCTYPE html>
<html>
<head>
<title>HTML menuitem Tag (Deprecated)</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<div style="border: 2px solid #333; padding: 30px; background-color: #f9f9f9;" contextmenu="clickmenu">
<p>Right click inside this box to see the context menu...</p>
<menu type="context" id="clickmenu">
<menuitem label="TutorialsPoint" onclick="alert('TutorialsPoint clicked!')"></menuitem>
<menuitem label="Tutorials Library" onclick="alert('Library clicked!')"></menuitem>
<menuitem label="Coding Ground" onclick="alert('Coding Ground clicked!')"></menuitem>
<menuitem label="Q/A" onclick="alert('Q/A clicked!')"></menuitem>
</menu>
</div>
<p><strong>Note:</strong> This feature is deprecated and not supported in modern browsers.</p>
</body>
</html>
This example would not work in current browsers as the <menuitem> and context menu functionality has been removed.
Modern Alternative − Custom Context Menu
Since <menuitem> is deprecated, developers now create custom context menus using JavaScript and CSS. Here's a modern approach −
<!DOCTYPE html>
<html>
<head>
<title>Custom Context Menu</title>
<style>
.context-area {
border: 2px solid #333;
padding: 30px;
background-color: #f9f9f9;
cursor: pointer;
user-select: none;
}
.context-menu {
position: absolute;
background: white;
border: 1px solid #ccc;
box-shadow: 2px 2px 10px rgba(0,0,0,0.3);
padding: 5px 0;
display: none;
z-index: 1000;
}
.context-menu-item {
padding: 8px 15px;
cursor: pointer;
transition: background-color 0.2s;
}
.context-menu-item:hover {
background-color: #f0f0f0;
}
</style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<div class="context-area" id="contextArea">
<p>Right click inside this box to see the custom context menu...</p>
</div>
<div class="context-menu" id="contextMenu">
<div class="context-menu-item" onclick="menuAction('TutorialsPoint')">TutorialsPoint</div>
<div class="context-menu-item" onclick="menuAction('Tutorials Library')">Tutorials Library</div>
<div class="context-menu-item" onclick="menuAction('Coding Ground')">Coding Ground</div>
<div class="context-menu-item" onclick="menuAction('Q/A')">Q/A</div>
</div>
<script>
const contextArea = document.getElementById('contextArea');
const contextMenu = document.getElementById('contextMenu');
contextArea.addEventListener('contextmenu', function(e) {
e.preventDefault();
contextMenu.style.display = 'block';
contextMenu.style.left = e.pageX + 'px';
contextMenu.style.top = e.pageY + 'px';
});
document.addEventListener('click', function() {
contextMenu.style.display = 'none';
});
function menuAction(item) {
alert(item + ' clicked!');
contextMenu.style.display = 'none';
}
</script>
</body>
</html>
Right-clicking in the designated area displays a custom context menu with clickable items that trigger JavaScript functions.
Custom context menu appears on right-click with options: - TutorialsPoint - Tutorials Library - Coding Ground - Q/A
Alternative − Dropdown Menu
For standard menu functionality, use regular HTML elements with JavaScript for interactivity −
<!DOCTYPE html>
<html>
<head>
<title>Alternative Dropdown Menu</title>
<style>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px rgba(0,0,0,0.2);
z-index: 1;
border: 1px solid #ddd;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
}
</style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<div class="dropdown">
<button class="dropbtn">Menu Options</button>
<div class="dropdown-content">
<a href="#">TutorialsPoint</a>
<a href="#">Tutorials Library</a>
<a href="#">Coding Ground</a>
<a href="#">Q/A</a>
</div>
</div>
</body>
</html>
This creates a functional dropdown menu that appears when hovering over the button.
[Menu Options] button (On hover, dropdown shows: TutorialsPoint, Tutorials Library, Coding Ground, Q/A)
Browser Support and Deprecation
The <menuitem> element was only partially implemented in Firefox and never gained widespread browser support. It was officially removed from the HTML specification and should not be used in production websites. Modern web development relies on custom JavaScript solutions for context menus and interactive menu systems.
Conclusion
The <menuitem> tag in HTML5 was designed to create context menu items but has been deprecated due to limited browser support. Modern alternatives include custom context menus built with JavaScript and CSS, or standard dropdown menus using hover effects and click handlers for better cross-browser compatibility.
