JqueryUI Interactions
- JqueryUI - Sortable
- JqueryUI - Selectable
- JqueryUI - Resizable
- JqueryUI - Droppable
- JqueryUI - Draggable
JqueryUI Widgets
- JqueryUI - Tooltip
- JqueryUI - Tabs
- JqueryUI - Spinner
- JqueryUI - Slider
- JqueryUI - Progressbar
- JqueryUI - Menu
- JqueryUI - Dialog
- JqueryUI - Datepicker
- JqueryUI - Button
- JqueryUI - Autocomplete
- JqueryUI - Accordion
JqueryUI Effects
- JqueryUI - Toggle Class
- JqueryUI - Toggle
- JqueryUI - Switch Class
- JqueryUI - Show
- JqueryUI - Remove Class
- JqueryUI - Hide
- JqueryUI - Effect
- JqueryUI - Color Animation
- JqueryUI - Add Class
JqueryUI Utilities
JqueryUI Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 inpne 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.
Syntax
The menu() method can be used in two forms −
Method
Method
$ (selector, context).menu (options) 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.
Syntax
$(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 psts the different options that can be used with this method −
Sr.No. | Option & Description |
---|---|
1 | This option if set to true disables the menu. By default its value is false. |
2 | This option sets the icons for submenus. By default its value is { submenu: "ui-icon-carat-1-e" }. |
3 | This option is a selector for the elements that serve as the menu container, including sub-menus. By default its value is ul. |
4 | 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" }. |
5 | This option is used to customize the ARIA roles used for the menu and menu items. By default its value is menu. |
Default Functionapty
The following example demonstrates a simple example of menu widget functionapty, passing no parameters to the menu() method.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Menu functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/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"> <p><a href = "#">Spring</a></p> <p><a href = "#">Hibernate</a></p> <p><a href = "#">Java</a> <ul> <p><a href = "#">Java IO</a></p> <p><a href = "#">Swing</a></p> <p><a href = "#">Jaspr Reports</a></p> </ul> </p> <p><a href = "#">JSF</a></p> <p><a href = "#">HTML5</a></p> </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 −
In the above example, you can see a themeable menu with mouse and keyboard interactions for navigation.
Use of icons and position
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 functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/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"> <p><a href = "#">Spring</a></p> <p><a href = "#">Hibernate</a></p> <p><a href = "#">Java</a> <ul> <p><a href = "#">Java IO</a></p> <p><a href = "#">Swing</a></p> <p><a href = "#">Jaspr Reports</a></p> </ul> </p> <p><a href = "#">JSF</a></p> <p><a href = "#">HTML5</a></p> </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 −
In the above example, you can see we have appped an icon image for the submenu pst and also changed the submenu position.
$ (selector, context).menu ("action", params) Method
The menu ("action", params) method can perform an action on menu elements, such as enabpng/disabpng 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.
Syntax
$(selector, context).menu ("action", params);;
The following table psts the different actions that can be used with this method −
Sr.No. | Action & Description |
---|---|
1 | 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. |
2 | This action closes the current active sub-menu. Where event is of type Event and represents what triggered the menu to collapse. |
3 | This action closes all the open submenus. |
4 | This action removes menu functionapty completely. This will return the element back to its pre-init state. This method does not accept any arguments. |
5 | This action disables the menu. This method does not accept any arguments. |
6 | This action enables the the menu. This method does not accept any arguments. |
7 | 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. |
8 | 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. |
9 | 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. |
10 | 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. |
11 | 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. |
12 | 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. |
13 | 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. |
14 | This action gets an object containing key/value pairs representing the current menu options hash. |
15 | 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. |
16 | 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. |
17 | This action moves active state to previous menu item. Where event is of type Event and represents what triggered the focus to move. |
18 | 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. |
19 | This action initiapzes sub-menus and menu items that have not already been initiapzed. This method does not accept any arguments. |
20 | 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. |
21 | This action returns a jQuery object containing the menu. This method does not accept any arguments. |
The following examples demonstrate how to use the actions given in the above table.
Use of disable method
The following example demonstrates the use of disable() method.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Menu functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/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"> <p><a href = "#">Spring</a></p> <p><a href = "#">Hibernate</a></p> <p><a href = "#">Java</a> <ul> <p><a href = "#">Java IO</a></p> <p><a href = "#">Swing</a></p> <p><a href = "#">Jaspr Reports</a></p> </ul> </p> <p><a href = "#">JSF</a></p> <p><a href = "#">HTML5</a></p> </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 −
In the above example, you can see the menu is disabled.
Use of focus and collapseAll methods
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 functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/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"> <p><a href = "#">Spring</a></p> <p><a href = "#">Hibernate</a></p> <p><a href = "#">JSF</a></p> <p><a href = "#">HTML5</a></p> <p><a href = "#">Java</a> <ul> <p><a href = "#">Java IO</a></p> <p><a href = "#">Swing</a></p> <p><a href = "#">Jaspr Reports</a></p> </ul> </p> </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 −
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.
Event Management on menu elements
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 psted below −
Sr.No. | Event Method & Description |
---|---|
1 | This event is triggered when a menu loses focus. |
2 | This event is triggered when a menu is created. |
3 | This event is triggered when a menu gains focus or when any menu item is activated. |
4 | This event is triggered when a menu item is selected. |
Example
The following example demonstrates the event method usage for menu widget functionapty. 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 functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/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"> <p><a href = "#">Spring</a></p> <p><a href = "#">Hibernate</a></p> <p><a href = "#">JSF</a></p> <p><a href = "#">HTML5</a></p> <p><a href = "#">Java</a> <ul> <p><a href = "#">Java IO</a></p> <p><a href = "#">Swing</a></p> <p><a href = "#">Jaspr Reports</a></p> </ul> </p> </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 −
In the above example, we are printing the messages based on the events triggered.
Advertisements