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.
There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.
The <menuitem>
element accepts the following attributes.
Element-Specific Attributes
This table shows the attributes that are specific to the <menuitem>
tag/element.
Attribute | Description |
---|---|
type | Specifies the type of command.
Possible values:
|
label | Specifies the name of the command, as shown to the user. |
icon | Specifies the URI (or IRI) of graphical image that represents the action. |
disabled | Specifies 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 Possible values:
|
checked | Indicates 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 Possible values:
|
radiogroup | Specifies 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". |
default | Specifies 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 Possible values:
|
command | Here's what the W3C HTML5 specification says about the command attribute:
|
Global Attributes
The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <menuitem>
tag , as well as with all other HTML tags.
- accesskey
- class
- contenteditable
- contextmenu
- dir
- draggable
- dropzone
- hidden
- id
- inert
- itemid
- itemprop
- itemref
- itemscope
- itemtype
- lang
- spellcheck
- style
- tabindex
- title
- translate
For a full explanation of these attributes, see HTML 5 global attributes.
Event Handler Content Attributes
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.
Below are the standard HTML5 event handler content attributes.
Again, you can use any of these with the <menuitem>
element, as well as any other HTML5 element.
- onabort
- oncancel
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragexit
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onformchange
- onforminput
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onpause
- onplay
- onplaying
- onprogress
- onratechange
- onreadystatechange
- onscroll
- onseeked
- onseeking
- onselect
- onshow
- onstalled
- onsubmit
- onsuspend
- ontimeupdate
- onvolumechange
- onwaiting
For a full explanation of these attributes, see HTML 5 event handler content attributes.
Differences Between HTML 4 & HTML 5
The <menuitem>
tag is not supported in HTML 4.
The <menuitem>
tag is not actually supported in the first version of HTML5 either. It is supported in HTML 5.1 and the WHATWG HTML Living Standard.
For more information on this element, see HTML5 <menuitem>
Tag. Also check out the links to the official specifications below.
Template
Here's a template for the <menuitem>
tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.
For more information on attributes for this tag, see HTML5 <menuitem>
Tag.
* The title
attribute has special semantics on the <menuitem>
element. If used, indicates the kind of command: either a normal command with an associated action, or a state or option that can be toggled, or a selection of one item from a list of items.
Tag Details
For more details about the <menuitem>
tag, see HTML5 <menuitem>
Tag.
Specifications
Here are the official specifications for the <menuitem>
element.
- HTML Living Standard (WHATWG)
- Current W3C Draft (the next version that is currently being worked on)
What's the Difference?
W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.