JqueryUI - Menu

A menu widget usually consists of a main menu bar with pop up menus. Items in pop up menus often have sub pop up menus. A menu can be created using the markup elements as long as the parent-child relation is maintained (using <ul> or <ol>). Each menu item has an anchor element.
The Menu Widget in jQueryUI can be used for inline and popup menus, or as a base for building more complex menu systems. For example, you can create nested menus with custom positioning.
jQueryUI provides menu() methods to create a menu.
The menu() method can be used in two forms −
$(selector, context).menu (options) Method
$(selector, context).menu (options) Method
$(selector, context).menu ("action", params) Method
$(selector, context).menu ("action", params) Method
The menu (options) method declares that an HTML element and its contents should be treated and managed as menus. The options parameter is an object that specifies the appearance and behavior of the menu items involved.
$(selector, context).menu (options);

You can provide one or more options at a time using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −
$(selector, context).menu({option1: value1, option2: value2..... });
The following table lists the different options that can be used with this method −
This option if set to true disables the menu. By default its value is false.
Option - disabled
This option if set to true disables the menu. By default its value is false.
Syntax
$( ".selector" ).menu (
   { disabled: true }
);

This option sets the icons for submenus. By default its value is { submenu: "ui-icon-carat-1-e" }.
Option - icons
This option sets the icons for submenus. By default its value is { submenu: "ui-icon-carat-1-e" }.
Syntax
$( ".selector" ).menu (
   { icons: { submenu: "ui-icon-circle-triangle-e" } }
);

This option is a selector for the elements that serve as the menu container, including sub-menus. By default its value is ul.
Option - menus
This option is a selector for the elements that serve as the menu container, including sub-menus. By default its value is ul.
Syntax
$( ".selector" ).menu (
   { menus: "div" }
);

This option sets the position of submenus in relation to the associated parent menu item. By default its value is { my: "left top", at: "right top" }.
Option - position	
This option sets the position of submenus in relation to the associated parent menu item. By default its value is { my: "left top", at: "right top" }.
Syntax
$( ".selector" ).menu (
   { position: { my: "left top", at: "right-5 top+5" } }
);

This option is used to customize the ARIA roles used for the menu and menu items. By default its value is menu.
Option - role
This option is used to customize the ARIA roles used for the menu and menu items. By default its value is menu.
Syntax
$( ".selector" ).menu (
   { role: null }
);

The following example demonstrates a simple example of menu widget functionality, passing no parameters to the menu() method.
<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Menu functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-menu {
            width: 200px;
         }
      </style>
      <!-- Javascript -->
      
      <script>
         $(function() {
            $( "#menu-1" ).menu();
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <ul id = "menu-1">
         <li><a href = "#">Spring</a></li>
         <li><a href = "#">Hibernate</a></li>
         <li><a href = "#">Java</a>
            <ul>
               <li><a href = "#">Java IO</a></li>
               <li><a href = "#">Swing</a></li>
               <li><a href = "#">Jaspr Reports</a></li>
            </ul>
         </li>
         <li><a href = "#">JSF</a></li>
         <li><a href = "#">HTML5</a></li>
      </ul>
   </body>
</html>
Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
Spring
Hibernate
Java

Java IO
Swing
Jaspr Reports


Java IO
Swing
Jaspr Reports
JSF
HTML5
In the above example, you can see a themeable menu with mouse and keyboard interactions for navigation.
The following example demonstrates the usage of two options icons, and position in the menu function of JqueryUI.
<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Menu functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-menu {
            width: 200px;
         }
      </style>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#menu-2" ).menu({
               icons: { submenu: "ui-icon-circle-triangle-e"},
               position: { my: "right top", at: "right-10 top+5" }
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <ul id = "menu-2">
         <li><a href = "#">Spring</a></li>
         <li><a href = "#">Hibernate</a></li>
         <li><a href = "#">Java</a>
            <ul>
               <li><a href = "#">Java IO</a></li>
               <li><a href = "#">Swing</a></li>
               <li><a href = "#">Jaspr Reports</a></li>
            </ul>
         </li>
         <li><a href = "#">JSF</a></li>
         <li><a href = "#">HTML5</a></li>
      </ul>
   </body>
</html>
Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
Spring
Hibernate
Java

Java IO
Swing
Jaspr Reports


Java IO
Swing
Jaspr Reports
JSF
HTML5
In the above example, you can see we have applied an icon image for the submenu list and also changed the submenu position.
The menu ("action", params) method can perform an action on menu elements, such as enabling/disabling the menu. The action is specified as a string in the first argument (e.g., "disable" disables the menu). Check out the actions that can be passed, in the following table.
$(selector, context).menu ("action", params);;

The following table lists the different actions that can be used with this method −
This action removes the focus from a menu. It triggers the menu's blur event by resetting any active element style. Where event is of type Event and represents what triggered the menu to blur.
Action - blur( [event ] )
This action removes the focus from a menu. It triggers the menu's blur event by resetting any active element style. Where event is of type Event and represents what triggered the menu to blur.
Syntax
$(".selector").menu( "blur" );

This action closes the current active sub-menu. Where event is of type Event and represents what triggered the menu to collapse.
Action - collapse( [event ] )
This action closes the current active sub-menu. Where event is of type Event and represents what triggered the menu to collapse.
Syntax
$(".selector").menu( "collapse" );

This action closes all the open submenus.
Action - collapseAll( [event ] [, all ] )
This action closes all the open submenus. Where −
event is of type Event and represents what triggered the menu to collapse
event is of type Event and represents what triggered the menu to collapse
all is of 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.
all is of 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.
Syntax
$(".selector").menu( "collapseAll", null, true );

This action removes menu functionality completely. This will return the element back to its pre-init state. This method does not accept any arguments.
Action - destroy()
This action removes menu functionality completely. This will return the element back to its pre-init state. This method does not accept any arguments.
Syntax
$(".selector").menu( "destroy" );

This action disables the menu. This method does not accept any arguments.
Action - disable()
This action disables the menu. This method does not accept any arguments.
Syntax
$(".selector").menu( "disable" );

This action enables the the menu. This method does not accept any arguments.
Action - enable()
This action enables the the menu. This method does not accept any arguments.
Syntax
$(".selector").menu( "enable" );

This action opens the sub-menu below the currently active item, if one exists. Where event is of type Event and represents what triggered the menu to expand.
Action - expand( [event ] )
This action opens the sub-menu below the currently active item, if one exists. Where event is of type Event and represents what triggered the menu to expand.
Syntax
$(".selector").menu( "expand" );

This action activates a particular menu item, begins opening any sub-menu if present and triggers the menu's focus event. Where event is of type Event and represents what triggered the menu to gain focus. and item is a jQuery object representing the menu item to focus/activate.
Action - focus( [event ], item )
This action activates a particular menu item, begins opening any sub-menu if present and triggers the menu's focus event. Where event is of type Event and represents what triggered the menu to gain focus. and item is a jQuery object representing the menu item to focus/activate.
Syntax
$(".selector").menu( "focus", null, menu.find( ".ui-menu-item:last" ) );

This action returns a boolean value, which states if the current active menu item is the first menu item. This method does not accept any arguments.
Action - isFirstItem()
This action returns a boolean value, which states if the current active menu item is the first menu item. This method does not accept any arguments.
Syntax
$(".selector").menu( "isFirstItem" );

This action returns a boolean value, which states if the current active menu item is the last menu item. This method does not accept any arguments.
Action - isLastItem()
This action returns a boolean value, which states if the current active menu item is the last menu item. This method does not accept any arguments.
Syntax
$(".selector").menu( "isLastItem" );

This action delegates the active state to the next menu item. Where event is of type Event and represents what triggered the focus to move.
Action - next( [event ] )
This action delegates the active state to the next menu item. Where event is of type Event and represents what triggered the focus to move.
Syntax
$(".selector").menu( "next" );

This action moves active state to first menu item below the bottom of a scrollable menu or the last item if not scrollable. Where event is of type Event and represents what triggered the focus to move.
Action - nextPage( [event ] )
This action moves active state to first menu item below the bottom of a scrollable menu or the last item if not scrollable. Where event is of type Event and represents what triggered the focus to move.
Syntax
$(".selector").menu( "nextPage" );

This action gets the value currently associated with the specified optionName. Where optionName is of type String and represents the name of the option to get.
Action - option( optionName )
This action gets the value currently associated with the specified optionName. Where optionName is of type String and represents the name of the option to get.
Syntax
var isDisabled = $( ".selector" ).menu( "option", "disabled" );

This action gets an object containing key/value pairs representing the current menu options hash.
Action - option()
This action gets an object containing key/value pairs representing the current menu options hash.
Syntax
var options = $( ".selector" ).menu( "option" );

This action sets the value of the menu option associated with the specified optionName. Where optionName is of type String and represents name of option to set and value is of type Object and represents value to set for the option.
Action - option( optionName, value )
This action sets the value of the menu option associated with the specified optionName. Where optionName is of type String and represents name of option to set and value is of type Object and represents value to set for the option.
Syntax
$(".selector").menu( "option", "disabled", true );

This action sets one or more options for the menu. Where options is of type Object and represents a map of option-value pairs to set.
Action - option( options )
This action sets one or more options for the menu. Where options is of type Object and represents a map of option-value pairs to set.
Syntax
$(".selector").menu( "option", { disabled: true } );

This action moves active state to previous menu item. Where event is of type Event and represents what triggered the focus to move.
Action - previous( [event ] )
This action moves active state to previous menu item. Where event is of type Event and represents what triggered the focus to move.
Syntax
$(".selector").menu( "previous" );

This action moves active state to first menu item above the top of a scrollable menu or the first item if not scrollable. Where event is of type Event and represents what triggered the focus to move.
Action - previousPage( [event ] )
This action moves active state to first menu item above the top of a scrollable menu or the first item if not scrollable. Where event is of type Event and represents what triggered the focus to move.
Syntax
$(".selector").menu( "previousPage" );

This action initializes sub-menus and menu items that have not already been initialized. This method does not accept any arguments.
Action - refresh()
This action initializes sub-menus and menu items that have not already been initialized. This method does not accept any arguments.
Syntax
$(".selector").menu( "refresh" );

This action selects the currently active menu item, collapses all sub-menus and triggers the menu's select event. Where event is of type Event and represents what triggered the selection.
Action - select( [event ] )
This action selects the currently active menu item, collapses all sub-menus and triggers the menu's select event. Where event is of type Event and represents what triggered the selection.
Syntax
$(".selector").menu( "select" );

This action returns a jQuery object containing the menu. This method does not accept any arguments.
Action - widget()
This action returns a jQuery object containing the menu. This method does not accept any arguments.
Syntax
$(".selector").menu( "widget" );

The following examples demonstrate how to use the actions given in the above table.
The following example demonstrates the use of disable() method.
<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Menu functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-menu {
            width: 200px;
         }
      </style>
      <!-- Javascript -->
      
      <script>
         $(function() {
            $( "#menu-3" ).menu();
            $( "#menu-3" ).menu("disable");
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <ul id = "menu-3">
         <li><a href = "#">Spring</a></li>
         <li><a href = "#">Hibernate</a></li>
         <li><a href = "#">Java</a>
            <ul>
               <li><a href = "#">Java IO</a></li>
               <li><a href = "#">Swing</a></li>
               <li><a href = "#">Jaspr Reports</a></li>
            </ul>
         </li>
         <li><a href = "#">JSF</a></li>
         <li><a href = "#">HTML5</a></li>
      </ul>
   </body>
</html>
Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
Spring
Hibernate
Java

Java IO
Swing
Jaspr Reports


Java IO
Swing
Jaspr Reports
JSF
HTML5
In the above example, you can see the menu is disabled.
The following example demonstrates the use of focus() and collapseAll methods.
<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Menu functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-menu {
            width: 200px;
         }
      </style>
      
      <!-- Javascript -->
      <script>
         $(function() {
            var menu = $("#menu-4").menu();
            $( "#menu-4" ).menu(
               "focus", null, $( "#menu-4" ).menu().find( ".ui-menu-item:last" ));
            $(menu).mouseleave(function () {
               menu.menu('collapseAll');
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <ul id = "menu-4">
         <li><a href = "#">Spring</a></li>
         <li><a href = "#">Hibernate</a></li>
         <li><a href = "#">JSF</a></li>
         <li><a href = "#">HTML5</a></li>
         <li><a href = "#">Java</a>
            <ul>
               <li><a href = "#">Java IO</a></li>
               <li><a href = "#">Swing</a></li>
               <li><a href = "#">Jaspr Reports</a></li>
            </ul>
         </li>
      </ul>
   </body>
</html>
Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
Spring
Hibernate
JSF
HTML5
Java

Java IO
Swing
Jaspr Reports


Java IO
Swing
Jaspr Reports
In the above example, you can see the focus is on the last menu item. Now expand the submenu and when the mouse leaves the submenu, the submenu is closed.
In addition to the menu (options) method which we saw in the previous sections, JqueryUI provides event methods which gets triggered for a particular event. These event methods are listed below −
This event is triggered when a menu loses focus.
Event - blur(event, ui)
This event is triggered when a menu loses focus. Where event is of type Event, and ui is of type Object and represents the currently active menu item.
Syntax
$( ".selector" ).menu({
   blur: function( event, ui ) {}
});

This event is triggered when a menu is created.
Event - create(event, ui)
This event is triggered when a menu is created. Where event is of type Event, and ui is of type Object.
Syntax
$( ".selector" ).menu({
   create: function( event, ui ) {}
});

This event is triggered when a menu gains focus or when any menu item is activated.
Event - focus(event, ui)
This event is triggered when a menu gains focus or when any menu item is activated. Where event is of type Event, and ui is of type Object and represents the currently active menu item.
Syntax
$( ".selector" ).menu({
   focus: function( event, ui ) {}
});

This event is triggered when a menu item is selected.
Event - select(event, ui)
This event is triggered when a menu item is selected. Where event is of type Event, and ui is of type Object and represents the currently active menu item.
Syntax
$( ".selector" ).menu({
   select: function( event, ui ) {}
});

The following example demonstrates the event method usage for menu widget functionality. This example demonstrates the use of event create, blur and focus.
<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Menu functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-menu {
            width: 200px;
         }
      </style>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#menu-5" ).menu({
               create: function( event, ui ) {
                  var result = $( "#result" );
                  result.append( "Create event<br>" );
               },
               blur: function( event, ui ) {
                  var result = $( "#result" );
                  result.append( "Blur event<br>" );
               },
               focus: function( event, ui ) {
                  var result = $( "#result" );
                  result.append( "focus event<br>" );
               }
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <ul id = "menu-5">
         <li><a href = "#">Spring</a></li>
         <li><a href = "#">Hibernate</a></li>
         <li><a href = "#">JSF</a></li>
         <li><a href = "#">HTML5</a></li>
         <li><a href = "#">Java</a>
            <ul>
               <li><a href = "#">Java IO</a></li>
               <li><a href = "#">Swing</a></li>
               <li><a href = "#">Jaspr Reports</a></li>
            </ul>
         </li>
      </ul>
      <span id = "result"></span>
   </body>
</html>
Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
Spring
Hibernate
JSF
HTML5
Java

Java IO
Swing
Jaspr Reports


Java IO
Swing
Jaspr Reports
In the above example, we are printing the messages based on the events triggered.
 Print
 Add Notes
 Bookmark this page