XUL Programmer's Reference Manual

<menuitem /> 

Attributes Common Children
value * * *
key  
acceltext  
accesskey  
disabled  
oncommand  
observes  
id  
class  
checked  
type  
name  
data  

<menuitem> is the widget that represents the actual item in the menu. menuitems typically appear as children of a menupopup whose own parent is a <menu>. The basic structure of a XUL menu is as follows:

 
<menu value="File">
    <menupopup>
<menuitem value="Open" />
       <menuitem value="Close" oncommand="window.close()"/>
       <menuseparator/>
       <menuitem value="Save" />
       <menuitem value="Exit"/>
    </menupopup>
</menu>
 
value
Description
The value attribute specifies the name displayed for the menuitem.
Syntax
<menuitem value="string" />
Example
<menuitem value="New" />
<menuitem value="Open" />
Notes
 
checked
Description
When checked is set to "true", the menu itself appears with a check mark next to it in the menubar.
 
<menu value="File">
  <menupopup>
    <menuitem value="New..."/>
    <menu value="Open"/>
    <menuitem value="Close"/>
    <menuseparator/>  
    <menuitem value="Save" disabled="true"/>
    <menuitem value="Save As..." checked="true"/>
    <menuseparator/>  
    <menuitem value="Exit"/>
  </menupopup>
</menu>
Syntax
checked="[true | false]"
Example
<menuitem value="AutoSave" checked="true" />
Notes
Menuitems can be checked in this way, or they can be created as part of a radio or checkbox menu type (see the type attribute below), in which case the checkmarks appear without having to set this attribute. You can check for the state of this attribute by using something like the following in javascript, where "autosave" is the id of the menuitem:

autosave_item = document.getElementById("autosave");
current_state = autosave_item.getAttribute("checked");
if (current_state = true) {
    doAutoSave()
}
 

 
oncommand
Description
oncommand is an optional attribute for creating event handlers for key bindings and menu commands.
Syntax
<menuitem id="string" value="string" oncommand="event handler code" />
Example
<menuitem id="saveKey" value="Save All" oncommand="SaveAll()" />
Notes
one.. 

 

 

type
Description
The typeattribute specifies the kind of menu that contains the given menuitem(s).
Syntax
<menuitem type="[ radio | checkbox ]" />
Example
<menuitem value="Auto Save" type="radio" name="save_type" />
<menuitem value="Manual Save" type="radio" name="save_type" />
Notes
In addition to the default menu type, which has no check marks or other indicators, you can also use the type attribute to specify menuitems of type radio and checkbox. Type, in this case, is a function of the menuitems and not the menu itself.

radio menu allows the user to check only a single menuitem at any time. When an item is selected from the radio menu, that item is checked, and any other item that was checked before is cleared. This functionality requires that all menuitems participating in this group be given the same name.
 
<menu value="Wash">
  <menupopup>
    <menuitem disabled="true" value="Special Wax" />
    <menuseparator />
    <menuitem type="radio" name="w1" value="Disinfect" />
    <menuitem type="radio" name="w1" value="Scrub" />
    <menuitem type="radio" name="w1" value="Hose Down" />
  </menupopup>
</menu>

Note that the first menuitem is not part of the radio set. When the user chooses "Scrub" from the Wash menu, the Disinfect item that was previously selected is deselected. In this scenario, the user can choose only one type of wash at a time.

A checkbox menu type also uses checks to indicate selected items, but it does not prohibit the selection of multiple items. When the example menu above is specified as a checkbox menu, the user can choose to activate a group of items.
 
<menu value="Wash">
  <menupopup>
    <menuitem disabled="true" value="Special Wax" />
    <menuseparator />
    <menuitem type="checkbox" value="Disinfect" />
    <menuitem type="checkbox" value="Scrub" />
    <menuitem type="checkbox" value="Hose Down" />
  </menupopup>
</menu>

Note that names are not necessary for checkbox menuitems, because the toggling of each item functions independently of the others.

name
Description
name is used to associated radio menuitems with one another.
Syntax
name="string"
Example
<menuitem value="Auto Save" type="radio" name="save_type" />
<menuitem value="Manual Save" type="radio" name="save_type" />
Notes
It is not necessary to use names with checkbox type menus, because the menuitems function independently of one another. Note also that you can name only a portion of the menuitems in a particular menu to make a set; not all menuittems within a particular menu need to be part of the same set.
data
Description
data is an optional attribute used to specify additional text associated with the menuitem.
Syntax
<menuitem data="string">
Example
<menuitem id="hidden" value="one thing" data="another thing"
Notes
The data attribute provides a convenient way to hold extra text for a menuitem, text that can be hidden from the user but accessed programmatically when necessary. For example, to access the data of the menuitem in the example above, simply get the menuitem and ask for the value of its data attribute:
 
var item = document.getElementById("hidden");
var hiddenText = item.getAttribute("data");

 
 
Last updated: 3/27/00 Ian Oeschger