QuickNav

Options

disabled
icons
menus
position
role

Methods

blur
collapse
collapseAll
destroy
disable
enable
expand
focus
isFirstItem
isLastItem
next
nextPage
option
previous
previousPage
refresh
select
widget

Events

blur
create
focus
select

Menu Widgetversion added: 1.9

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

Options

disabledType: Boolean

Default: false
Disables the menu if set to true.

iconsType: Object

Default: { submenu: "ui-icon-carat-1-e" }
Icons to use for submenus, matching an icon defined by the jQuery UI CSS Framework.
  • submenu (string, default: "ui-icon-carat-1-e")

menusType: String

Default: "ul"
Selector for the elements that serve as the menu container, including sub-menus.

positionType: Object

Default: { my: "top left", at: "top right" }
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.

roleType: 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.

Methods

blur( [event ] )

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.

collapse( [event ] )

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

collapseAll( [event ] [, all ] )

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.

destroy()

Removes the menu functionality completely. This will return the element back to its pre-init state.

disable()

Disables the menu.

enable()

Enables the menu.

expand( [event ] )

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

focus( [event ] [, item ] )

Activates a particular menu item, begins opening any sub-menu if present and triggers the menu's focus event.
  • event
    Type: Event
    What triggered the menu item to gain focus.
  • item
    Type: jQuery
    The menu item to focus/activate.

isFirstItem()

Returns a boolean value stating whether or not the currently active item is the first item in the menu.

isLastItem()

Returns a boolean value stating whether or not the currently active item is the last item in the menu.

next( [event ] )

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

nextPage( [event ] )

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.

option( optionName ) Returns: Object

Gets the value currently associated with the specified optionName.
  • optionName
    Type: String
    The name of the option to get.

option() Returns: PlainObject

Gets an object containing key/value pairs representing the current menu options hash.

option( optionName, value )

Sets the value of the menu option associated with the specified optionName.
  • optionName
    Type: String
    The name of the option to set.
  • value
    Type: Object
    A value to set for the option.

option( options )

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

previous( [event ] )

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

previousPage( [event ] )

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.

refresh()

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.

select( [event ] )

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

widget() Returns: jQuery

Returns a jQuery object containing the menu.

Events

blur( event, ui )

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

create( event, ui )

Triggered when the menu is created.

focus( event, ui )

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.

select( event, ui )

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

A menu can be created from any valid markup as long as the elements have a strict parent/child relationship and each menu item has an anchor. The most commonly used element is the unordered list (<ul>):

<ul id="menu">
	<li><a href="#">Item 1</a></li>
	<li><a href="#">Item 2</a></li>
	<li><a href="#">Item 3</a>
		<ul>
			<li><a href="#">Item 3-1</a></li>
			<li><a href="#">Item 3-2</a></li>
			<li><a href="#">Item 3-3</a></li>
			<li><a href="#">Item 3-4</a></li>
			<li><a href="#">Item 3-5</a></li>
		</ul>
	</li>
	<li><a href="#">Item 4</a></li>
	<li><a href="#">Item 5</a></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.

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.

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.

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
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>menu demo</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css">
    <style>
    .ui-menu {
        width: 200px;
    }
    </style>
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
</head>
<body>
 
<ul id="menu">
    <li><a href="#">Item 1</a></li>
    <li><a href="#">Item 2</a></li>
    <li><a href="#">Item 3</a>
        <ul>
            <li><a href="#">Item 3-1</a></li>
            <li><a href="#">Item 3-2</a></li>
            <li><a href="#">Item 3-3</a></li>
            <li><a href="#">Item 3-4</a></li>
            <li><a href="#">Item 3-5</a></li>
        </ul>
    </li>
    <li><a href="#">Item 4</a></li>
    <li><a href="#">Item 5</a></li>
</ul>
 
<script>
$( "#menu" ).menu();
</script>
 
</body>
</html>

Demo: