Menu Widget


Menu Widgetversion added: 1.9

Description: Themeable menu with mouse and keyboard interactions for navigation.

QuickNavExamples

A menu can be created from any valid markup as long as the elements have a strict parent/child relationship. The most commonly used element is the unordered list (<ul>). Additionally, the contents of each menu item must be wrapped with a block-level DOM element. In the example below <div> elements are used as wrappers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<ul id="menu">
<li>
<div>Item 1</div>
</li>
<li>
<div>Item 2</div>
</li>
<li>
<div>Item 3</div>
<ul>
<li>
<div>Item 3-1</div>
</li>
<li>
<div>Item 3-2</div>
</li>
<li>
<div>Item 3-3</div>
</li>
</ul>
</li>
<li>
<div>Item 4</div>
</li>
<li>
<div>Item 5</div>
</li>
</ul>

If you use a structure other than <ul>/<li>, including using the same element for the menu and the menu items, use the menus option to specify a way to differentiate the two elements, e.g., menus: "div.menuElement".

Any menu item can be disabled by adding the ui-state-disabled class to that element.

Icons

To add icons to the menu, include them in the markup:

1
2
3
4
5
<ul id="menu">
<li>
<div><span class="ui-icon ui-icon-disk"></span>Save</div>
</li>
</ul>

Menu automatically adds the necessary padding to items without icons.

Dividers

Divider elements can be created by including menu items that contain only spaces and/or dashes:

1
2
3
4
5
6
7
8
9
<ul id="menu">
<li>
<div>Item 1</div>
</li>
<li>-</li>
<li>
<div>Item 2</div>
</li>
</ul>

Keyboard interaction

  • ENTER/SPACE: Invoke the focused menu item's action, which may be opening a submenu.
  • UP: Move focus to the previous menu item.
  • DOWN: Move focus to the next menu item.
  • RIGHT: Open the submenu, if available.
  • LEFT: Close the current submenu and move focus to the parent menu item. If not in a submenu, do nothing.
  • ESCAPE: Close the current submenu and move focus to the parent menu item. If not in a submenu, do nothing.

Typing a letter moves focus to the first item whose title starts with that character. Repeating the same character cycles through matching items. Typing more characters within the one second timer matches those characters.

Disabled items can receive keyboard focus, but do not allow any other interaction.

Theming

The menu widget uses the jQuery UI CSS framework to style its look and feel. If menu specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes option:

  • ui-menu: The outer container of the menu, as well as any nested submenu. This top-level element will additionally have a ui-menu-icons class if the menu contains icons.
    • ui-menu-item: The container for individual menu items. This contains the element for the item's text itself as well as the element for submenus.
      • ui-menu-item-wrapper: The wrapper element inside each individual menu item, containting the text content and the icon indicating submenus.
        • ui-menu-icon: The submenu icons set via the icons option.
    • ui-menu-divider: Divider elements between menu items.

Dependencies

Additional Notes:

  • This widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS file as a starting point.

Options

classes 

Type: Object
Default: {}

Specify additional classes to add to the widget's elements. Any of classes specified in the Theming section can be used as keys to override their value. To learn more about this option, check out the learn article about the classes option.

Code examples:

Initialize the menu with the classes option specified, changing the theming for the ui-menu class:

1
2
3
4
5
$( ".selector" ).menu({
classes: {
"ui-menu": "highlight"
}
});

Get or set a property of the classes option, after initialization, here reading and changing the theming for the ui-menu class:

1
2
3
4
5
// Getter
var themeClass = $( ".selector" ).menu( "option", "classes.ui-menu" );
// Setter
$( ".selector" ).menu( "option", "classes.ui-menu", "highlight" );

disabled 

Type: Boolean
Default: false
Disables the menu if set to true.
Code examples:

Initialize the menu with the disabled option specified:

1
2
3
$( ".selector" ).menu({
disabled: true
});

Get or set the disabled option, after initialization:

1
2
3
4
5
// Getter
var disabled = $( ".selector" ).menu( "option", "disabled" );
// Setter
$( ".selector" ).menu( "option", "disabled", true );

icons 

Type: Object
Default:
{
submenu: "ui-icon-carat-1-e"
}
Icons to use for submenus, matching an icon provided by the jQuery UI CSS Framework.
Code examples:

Initialize the menu with the icons option specified:

1
2
3
$( ".selector" ).menu({
icons: { submenu: "ui-icon-circle-triangle-e" }
});

Get or set the icons option, after initialization:

1
2
3
4
5
// Getter
var icons = $( ".selector" ).menu( "option", "icons" );
// Setter
$( ".selector" ).menu( "option", "icons", { submenu: "ui-icon-circle-triangle-e" } );

items 

Type: String
Default: "> *"

Selector for the elements that serve as the menu items.

Note: The items option should not be changed after initialization.
(version added: 1.11.0)
Code examples:

Initialize the menu with the items option specified:

1
2
3
$( ".selector" ).menu({
items: ".custom-item"
});

Get the items option, after initialization:

1
2
// Getter
var items = $( ".selector" ).menu( "option", "items" );

menus 

Type: String
Default: "ul"

Selector for the elements that serve as the menu container, including sub-menus.

Note: The menus option should not be changed after initialization. Existing submenus will not be updated.
Code examples:

Initialize the menu with the menus option specified:

1
2
3
$( ".selector" ).menu({
menus: "div"
});

Get the menus option, after initialization:

1
2
// Getter
var menus = $( ".selector" ).menu( "option", "menus" );

position 

Type: Object
Default: { my: "left top", at: "right top" }
Identifies the position of submenus in relation to the associated parent menu item. The of option defaults to the parent menu item, but you can specify another element to position against. You can refer to the jQuery UI Position utility for more details about the various options.
Code examples:

Initialize the menu with the position option specified:

1
2
3
$( ".selector" ).menu({
position: { my: "left top", at: "right-5 top+5" }
});

Get or set the position option, after initialization:

1
2
3
4
5
// Getter
var position = $( ".selector" ).menu( "option", "position" );
// Setter
$( ".selector" ).menu( "option", "position", { my: "left top", at: "right-5 top+5" } );

role 

Type: String
Default: "menu"

Customize the ARIA roles used for the menu and menu items. The default uses "menuitem" for items. Setting the role option to "listbox" will use "option" for items. If set to null, no roles will be set, which is useful if the menu is being controlled by another element that is maintaining focus.

Note: The role option should not be changed after initialization. Existing (sub)menus and menu items will not be updated.
Code examples:

Initialize the menu with the role option specified:

1
2
3
$( ".selector" ).menu({
role: null
});

Get the role option, after initialization:

1
2
// Getter
var role = $( ".selector" ).menu( "option", "role" );

Methods

blur( [event ] )Returns: jQuery (plugin only)

Removes focus from a menu, resets any active element styles and triggers the menu's blur event.
  • event
    Type: Event
    What triggered the menu to blur.
Code examples:

Invoke the blur method:

1
$( ".selector" ).menu( "blur" );

collapse( [event ] )Returns: jQuery (plugin only)

Closes the currently active sub-menu.
  • event
    Type: Event
    What triggered the menu to collapse.
Code examples:

Invoke the collapse method:

1
$( ".selector" ).menu( "collapse" );

collapseAll( [event ] [, all ] )Returns: jQuery (plugin only)

Closes all open sub-menus.
  • event
    Type: Event
    What triggered the menu to collapse.
  • all
    Type: Boolean
    Indicates whether all sub-menus should be closed or only sub-menus below and including the menu that is or contains the target of the triggering event.
Code examples:

Invoke the collapseAll method:

1
$( ".selector" ).menu( "collapseAll", null, true );

destroy()Returns: jQuery (plugin only)

Removes the menu functionality completely. This will return the element back to its pre-init state.
  • This method does not accept any arguments.
Code examples:

Invoke the destroy method:

1
$( ".selector" ).menu( "destroy" );

disable()Returns: jQuery (plugin only)

Disables the menu.
  • This method does not accept any arguments.
Code examples:

Invoke the disable method:

1
$( ".selector" ).menu( "disable" );

enable()Returns: jQuery (plugin only)

Enables the menu.
  • This method does not accept any arguments.
Code examples:

Invoke the enable method:

1
$( ".selector" ).menu( "enable" );

expand( [event ] )Returns: jQuery (plugin only)

Opens the sub-menu below the currently active item, if one exists.
  • event
    Type: Event
    What triggered the menu to expand.
Code examples:

Invoke the expand method:

1
$( ".selector" ).menu( "expand" );

focus( [event ], item )Returns: jQuery (plugin only)

Activates the given menu item and triggers the menu's focus event. Opens the menu item's sub-menu, if one exists.
  • event
    Type: Event
    What triggered the menu item to gain focus.
  • item
    Type: jQuery
    The menu item to focus/activate.
Code examples:

Invoke the focus method:

1
$( ".selector" ).menu( "focus", null, menu.find( ".ui-menu-item:last" ) );

instance()Returns: Object

Retrieves the menu's instance object. If the element does not have an associated instance, undefined is returned.

Unlike other widget methods, instance() is safe to call on any element after the menu plugin has loaded.

  • This method does not accept any arguments.
Code examples:

Invoke the instance method:

1
$( ".selector" ).menu( "instance" );

isFirstItem()Returns: jQuery (plugin only)

Returns a boolean value stating whether or not the currently active item is the first item in the menu.
  • This method does not accept any arguments.
Code examples:

Invoke the isFirstItem method:

1
var firstItem = $( ".selector" ).menu( "isFirstItem" );

isLastItem()Returns: jQuery (plugin only)

Returns a boolean value stating whether or not the currently active item is the last item in the menu.
  • This method does not accept any arguments.
Code examples:

Invoke the isLastItem method:

1
var lastItem = $( ".selector" ).menu( "isLastItem" );

next( [event ] )Returns: jQuery (plugin only)

Moves active state to next menu item.
  • event
    Type: Event
    What triggered the focus to move.
Code examples:

Invoke the next method:

1
$( ".selector" ).menu( "next" );

nextPage( [event ] )Returns: jQuery (plugin only)

Moves active state to first menu item below the bottom of a scrollable menu or the last item if not scrollable.
  • event
    Type: Event
    What triggered the focus to move.
Code examples:

Invoke the nextPage method:

1
$( ".selector" ).menu( "nextPage" );

option( optionName )Returns: Object

Gets the value currently associated with the specified optionName.

Note: For options that have objects as their value, you can get the value of a specific key by using dot notation. For example, "foo.bar" would get the value of the bar property on the foo option.

  • optionName
    Type: String
    The name of the option to get.
Code examples:

Invoke the method:

1
var isDisabled = $( ".selector" ).menu( "option", "disabled" );

option()Returns: PlainObject

Gets an object containing key/value pairs representing the current menu options hash.
  • This signature does not accept any arguments.
Code examples:

Invoke the method:

1
var options = $( ".selector" ).menu( "option" );

option( optionName, value )Returns: jQuery (plugin only)

Sets the value of the menu option associated with the specified optionName.

Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName. For example, "foo.bar" would update only the bar property of the foo option.

  • optionName
    Type: String
    The name of the option to set.
  • value
    Type: Object
    A value to set for the option.
Code examples:

Invoke the method:

1
$( ".selector" ).menu( "option", "disabled", true );

option( options )Returns: jQuery (plugin only)

Sets one or more options for the menu.
  • options
    Type: Object
    A map of option-value pairs to set.
Code examples:

Invoke the method:

1
$( ".selector" ).menu( "option", { disabled: true } );

previous( [event ] )Returns: jQuery (plugin only)

Moves active state to previous menu item.
  • event
    Type: Event
    What triggered the focus to move.
Code examples:

Invoke the previous method:

1
$( ".selector" ).menu( "previous" );

previousPage( [event ] )Returns: jQuery (plugin only)

Moves active state to first menu item above the top of a scrollable menu or the first item if not scrollable.
  • event
    Type: Event
    What triggered the focus to move.
Code examples:

Invoke the previousPage method:

1
$( ".selector" ).menu( "previousPage" );

refresh()Returns: jQuery (plugin only)

Initializes sub-menus and menu items that have not already been initialized. New menu items, including sub-menus can be added to the menu or all of the contents of the menu can be replaced and then initialized with the refresh() method.
  • This method does not accept any arguments.
Code examples:

Invoke the refresh method:

1
$( ".selector" ).menu( "refresh" );

select( [event ] )Returns: jQuery (plugin only)

Selects the currently active menu item, collapses all sub-menus and triggers the menu's select event.
  • event
    Type: Event
    What triggered the selection.
Code examples:

Invoke the select method:

1
$( ".selector" ).menu( "select" );

widget()Returns: jQuery

Returns a jQuery object containing the menu.
  • This method does not accept any arguments.
Code examples:

Invoke the widget method:

1
var widget = $( ".selector" ).menu( "widget" );

Extension Points

The menu widget is built with the widget factory and can be extended. When extending widgets, you have the ability to override or add to the behavior of existing methods. The following methods are provided as extension points with the same API stability as the plugin methods listed above. For more information on widget extensions, see Extending Widgets with the Widget Factory.


_closeOnDocumentClick( event )Returns: Boolean

Method that determines whether clicks on the document should close any open menus. By default, menus are closed unless the click occurred on the menu.
Code examples:

Never close menus on document click.

1
2
3
_closeOnDocumentClick: function( event ) {
return false;
}

_isDivider( item )Returns: Boolean

Determines whether an item should actually be represented as a divider instead of a menu item. By default any item that contains just spaces and/or dashes is considered a divider.
Code examples:

Treat all items as menu items with no dividers.

1
2
3
_isDivider: function( item ) {
return false;
}

Events

blur( event, ui )Type: menublur

Triggered when the menu loses focus.
  • event
    Type: Event
  • ui
    Type: Object
    • item
      Type: jQuery
      The currently active menu item.
Code examples:

Initialize the menu with the blur callback specified:

1
2
3
$( ".selector" ).menu({
blur: function( event, ui ) {}
});

Bind an event listener to the menublur event:

1
$( ".selector" ).on( "menublur", function( event, ui ) {} );

create( event, ui )Type: menucreate

Triggered when the menu is created.

Note: The ui object is empty but included for consistency with other events.

Code examples:

Initialize the menu with the create callback specified:

1
2
3
$( ".selector" ).menu({
create: function( event, ui ) {}
});

Bind an event listener to the menucreate event:

1
$( ".selector" ).on( "menucreate", function( event, ui ) {} );

focus( event, ui )Type: menufocus

Triggered when a menu gains focus or when any menu item is activated.
  • event
    Type: Event
  • ui
    Type: Object
    • item
      Type: jQuery
      The currently active menu item.
Code examples:

Initialize the menu with the focus callback specified:

1
2
3
$( ".selector" ).menu({
focus: function( event, ui ) {}
});

Bind an event listener to the menufocus event:

1
$( ".selector" ).on( "menufocus", function( event, ui ) {} );

select( event, ui )Type: menuselect

Triggered when a menu item is selected.
  • event
    Type: Event
  • ui
    Type: Object
    • item
      Type: jQuery
      The currently active menu item.
Code examples:

Initialize the menu with the select callback specified:

1
2
3
$( ".selector" ).menu({
select: function( event, ui ) {}
});

Bind an event listener to the menuselect event:

1
$( ".selector" ).on( "menuselect", function( event, ui ) {} );

Example:

A simple jQuery UI Menu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>menu demo</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<style>
.ui-menu {
width: 200px;
}
</style>
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<ul id="menu">
<li>
<div>Item 1</div>
</li>
<li>
<div>Item 2</div>
</li>
<li>
<div>Item 3</div>
<ul>
<li>
<div>Item 3-1</div>
</li>
<li>
<div>Item 3-2</div>
</li>
<li>
<div>Item 3-3</div>
</li>
</ul>
</li>
<li>
<div>Item 4</div>
</li>
<li>
<div>Item 5</div>
</li>
</ul>
<script>
$( "#menu" ).menu();
</script>
</body>
</html>

Demo: