Before reading this article, I highly recommend reading the previous parts of
the series:
A menu widget provides mouse and keyboard interactions for navigation. A menu widget usually consists of a main menu bar with pop up menus. Each pop menu may have a sub pop menu. A menu may be created by list element (ul or ol) or may be created using other markup elements.
Syntax:
$(selector, context).menu (options)
or
$(selector, context).menu ("action", params)
Example
Output
Option
Option | Description | Example |
isable | If set to set disables the menu | $("#menu-1").menu({ disabled: true }); |
icon | Define icon used for menu | $("#menu-1").menu({ icons: { submenu: "ui-icon-transferthick-e-w" }, }); |
items | Define selector for the elements that serve as the menu items. Default value is “> *” | $("#menu-1").menu({ items: "*" }); |
menus | Define a selector for the elements that serve as the menu container, including sub-menus. By default its value is ul. | $("#menu-1").menu({ menus: "div" }); |
position | Define the position of submenus in relation to the associated parent menu item. By default value is { my: "left top", at: "right top" } | $("#menu-1").menu({ position:{ my: "left top", at: "left top" } }); |
role | Define ARIA roles used for the menu and menu items. By default its value is menu. | $("#menu-1").menu({ role: null }); |
Methods
Method | Description | Example |
blur(event) | Removes focus from a menu and resets any active element styles | $("#menu-1").menu("blur"); |
collapse(event) | Closes the currently active sub-menu. | $("#menu-1").menu("collapse"); |
expand(event) | Opens the sub-menu below the currently active item, if one exists. | $("#menu-1").menu("expand"); |
focus(event,item) | This method activates a particular menu item, begins opening any sub-menu if present and triggers the menu's focus event. | $("#menu-1").menu("focus", null, menu.find(".ui-menu-item:first")); |
isFirstItem() | This method returns a boolean value, if the current active menu item is the first menu item. | varobj=$("#menu-1").menu("isFirstItem"); |
isLastItem() | This method returns a boolean value, if the current active menu item is the last menu item. | varobj=$("#menu-1").menu("isLastItem"); |
nextPage(event) | This method moves active state to first menu item below the bottom of a scrollable menu or the last item if not scrollable | $("#menu-1").menu("nextPage"); |
previos(event) | This method moves active state to previous menu item. | $("#menu-1").menu("previous"); |
previousPage(event) | This method moves active state to first menu item above the top of a scrollable menu or the first item if not scrollable. | $("#menu-1").menu("previousPage"); |
referesh() | This method initializes sub-menus and menu items that have not already been initialized. | $("#menu-1").menu("refresh"); |
select() | This method selects the currently active menu item, collapses all sub-menus and triggers the menu's select event. | $("#menu-1").menu("select"); |
Events:
Event | Description | Example |
blur(event,ui) | Event triggered when the menu loses focus. | $("#menu-1").menu({ blur: function (event, ui) { } }); |
create(event,ui) | Event triggered when the menu is created. | $("#menu-1").menu({ create: function (event, ui) { } }); |
focus(event,ui) | Event triggered when a menu gains focus or when any menu item is activated. | $("#menu-1").menu({ focus: function (event, ui) { } }); |
select(event,ui) | Event triggered when a menu item is selected. | $("#menu-1").menu({ select: function (event, ui) { } }); |
Let us take some example:
Example 1
Output
In above example we used the “ui-state-disabled” class for li(rajasthan) item. This class is used to disable any item in the menu.
Example 2
Output
Item option defines selector for the elements that serve as the menu items. In above example we used the item option to create the different category of states.