HTML DOM MenuItem Object


The HTML DOM MenuItem Object in HTML represents the <menuitem> element.

NOTE<menuitem> element only supported in Mozilla Firefox Browser.

Syntax

Following is the syntax −

Creating a <menuitem> element

var menuItemObject = document.createElement(“MENUITEM”)

Properties

Here, menuItemobject can have the following properties −

PropertyDescription
checkedIt sets/returns (true/false) if the menu item should be checked
commandIt sets/returns the value of the command attribute
defaultIt sets/returns whether the menuItem should be the default
disabledIt sets/returns whether the menu item should be disabled
iconIt sets/returns an image that represents the menuItem
labelIt sets/returns the value of the label attribute of the menuItem
radiogroupIt sets/returns the value of the radiogroup attribute of the menuItem
typeIt sets/returns the value of the type attribute of the menuItem

Example

Let us see an example for HTML DOM MenuItem element −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Menu Object</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
   text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form contextmenu="MENU">
<fieldset>
<legend>Menu-Object</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="30" id="urlSelect" value="https://www.example.com/aboutUs">
<div id="divDisplay"></div>
</fieldset>
<menu type="context"
id="MENU">
<menuitem label="Get URL"
onclick="contextFunction(1);">
</menuitem>
<menuitem label="Get Hostname"
onclick="contextFunction(2);">
</menuitem>
</menu>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var urlSelect = document.getElementById("urlSelect");
   function gethref(){
      divDisplay.textContent = 'URL Path: '+location.href;
   }
   function getHostname(){
      divDisplay.textContent = 'Hostname: '+location.hostname;
   }
   function contextFunction(role){
      if(role === 1)
         this.gethref();
      else
         this.getHostname();
   }
</script>
</body>
</html>

Output

This will produce the following output −

Right clicking on form −

After clicking ‘Get Url’ menu item in context menu −

After clicking ‘Get Hostname’ menu item in context menu −

Updated on: 31-Jul-2019

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements