HTML <menuitem> Tag

This tag has now been removed from the HTML5 specification. Rather than remove this article altogether, I have decided to keep it here for reference.

The HTML <menuitem> tag represents a command that the user can invoke from a popup menu (either a context menu or the menu of a menu button).

You can use one or more type, label, icon, disabled, checked, and radiogroup attributes to set a new command. Otherwise you can use the command attribute to reference another command. This way, you can define a command once, set its state (e.g. whether it is active or disabled) in one place, and have all references to that command in the user interface change at the same time.

Syntax

The <menuitem> tag is written as <menuitem> (no end tag) with its contents inserted between the start and end tags. The tag can have any number of attributes, which are inserted into the start tag.

The <menuitem> tag must be a child of a <menu> element whose type attribute is in the popup menu state.

Like this:

Example

Context Menu

Here's an example of using the <menuitem> tag to create a context menu. Right-clicking on the photo will invoke the browser's contextual menu. However, a new option will be added to the browser's contextual menu: "Change Border Color...". The user can then select from the list of colors.

Attributes

Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

The <menuitem> element accepts the following attributes.

AttributeDescription
typeSpecifies the type of command.

Possible values:

  • command (default value). This value means that element represents a normal command with an associated action.
  • checkbox The element represents a state or option that can be toggled.
  • radio Means that the element represents a selection of one item from a list of items.
labelSpecifies the name of the command, as shown to the user.
iconSpecifies the URI (or IRI) of graphical image that represents the action.
disabledSpecifies if the command is disabled or not.

This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either disabled or disabled="disabled").

Possible values:

  • [Empty string]
  • disabled
checkedIndicates whether the command is selected or not. This attribute can only be used when the type attribute is either checkbox or radio.

This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either checked or checked="checked").

Possible values:

  • [Empty string]
  • checked
radiogroupSpecifies the name of the group of commands that will be toggled when the command itself is toggled, for commands whose type attribute has the value "radio".

defaultSpecifies that the command is the one that would have been invoked if the user had directly activated the menu's subject instead of using the menu.

This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either default or default="default").

Possible values:

  • [Empty string]
  • default
commandHere's what the W3C HTML5 specification says about the command attribute:

If the element's Disabled State is false (enabled) then the element's activation behavior depends on the element's type and command attributes, as follows:

If the element has a master command set by its command attribute
The user agent must run synthetic click activation steps on the element's master command.

If the type attribute is in the Checkbox state
If the element has a checked attribute, the UA must remove that attribute. Otherwise, the UA must add a checked attribute, with the literal value checked.

If the type attribute is in the Radio state
If the element has a parent, then the UA must walk the list of child nodes of that parent element, and for each node that is a menuitem element, if that element has a radiogroup attribute whose value exactly matches the current element's (treating missing radiogroup attributes as if they were the empty string), and has a checked attribute, must remove that attribute.

Then, the element's checked attribute must be set to the literal value checked.

Otherwise
The element's activation behavior is to do nothing.

Global Attributes

The following attributes are standard across all HTML elements. Therefore, you can use these attributes with the <menuitem> tag , as well as with all other HTML tags.

For a full explanation of these attributes, see HTML 5 global attributes.

Event Handlers

Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.

Most event handler content attributes can be used on all HTML elements, but some event handlers have specific rules around when they can be used and which elements they are applicable to.

For more detail, see HTML event handler content attributes.