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

  1. <!DOCTYPEhtml>
  2. <head runat="server">
  3. <title></title>
  4. <link href="http://code.jquery.com/ui/1.10.4/themes/blitzer/jquery-ui.css" rel="stylesheet">
  5. <script src="http://code.jquery.com/jquery-1.10.2.js">
  6. </script>
  7. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js">
  8. </script>
  9. <script>
  10. $(document).ready(function() {
  11. $("#menu-1").menu({
  12. icons: {
  13. submenu: "ui-icon-transferthick-e-w"
  14. },
  15. "select": function(event, ui) {
  16. alert(ui.item.text());
  17. }
  18. });
  19. });
  20. </script>
  21. <style>
  22. .ui-menu {
  23. width: 180px;
  24. margin-left: 400px;
  25. font-size: 24px;
  26. margin-top: 50px
  27. }
  28. #div1 {
  29. background-color: coral;
  30. color: ButtonHighlight;
  31. font-size: 30px;
  32. height: 450px;
  33. width: 1000px;
  34. text-align: center;
  35. margin-left: 150px
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div id="div1">
  41. <span>************Menu*************</span>
  42. <ul id="menu-1">
  43. <li>
  44. <a href="#">Element1</a>
  45. </li>
  46. <li>
  47. <a href="#">Element2</a>
  48. </li>
  49. <li>
  50. <a href="#">Element3</a>
  51. <ul>
  52. <li>
  53. <a href="#">Element3-1</a>
  54. </li>
  55. <li>
  56. <a href="#">Element3-2</a>
  57. </li>
  58. <li>
  59. <a href="#">Element3-3</a>
  60. </li>
  61. </ul>
  62. </li>
  63. <li>
  64. <a href="#">Element4</a>
  65. </li>
  66. </ul>
  67. </div>
  68. </body>
  69. </html>

Output

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

  1. <!DOCTYPE html>
  2. <head runat="server">
  3. <title></title>
  4. <link href="http://code.jquery.com/ui/1.10.4/themes/hot-sneaks/jquery-ui.css" rel="stylesheet">
  5. <script src="http://code.jquery.com/jquery-1.10.2.js">
  6. </script>
  7. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js">
  8. </script>
  9. <script>
  10. $(document).ready(function() {
  11. $("#menu").menu({
  12. icons: {
  13. submenu: "ui-icon-transferthick-e-w"
  14. },
  15. });
  16. });
  17. </script>
  18. <style>
  19. .ui-menu {
  20. width: 180px;
  21. margin-left: 400px;
  22. font-size: 24px;
  23. margin-top: 50px
  24. }
  25. #div1 {
  26. background-color: coral;
  27. color: ButtonHighlight;
  28. font-size: 30px;
  29. height: 450px;
  30. width: 1000px;
  31. text-align: center;
  32. margin-left: 150px
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div id="div1">
  38. <span>************Menu*************</span>
  39. <ul id="menu">
  40. <li>
  41. <a href="#">Bihar</a>
  42. </li>
  43. <li>
  44. <a href="#">Assam</a>
  45. <ul>
  46. <li>
  47. <a href="#">Dhubri</a>
  48. </li>
  49. <li>
  50. <a href="#">Goalpara</a>
  51. </li>
  52. <li>
  53. <a href="#">Golaghat</a>
  54. </li>
  55. </ul>
  56. </li>
  57. <li>
  58. <a href="#">Haryana</a>
  59. <ul>
  60. <li>
  61. <a href="#">Bhiwani</a>
  62. </li>
  63. <li>
  64. <a href="#">Gurgaon</a>
  65. </li>
  66. <li>
  67. <a href="#">Hisar</a>
  68. </li>
  69. </ul>
  70. </li>
  71. <li class='ui-state-disabled'>
  72. <a href="#">Rajasthan</a>
  73. </li>
  74. <li>
  75. <a href="#">Goa</a>
  76. <ul>
  77. <li>
  78. <a href="#">North Goa</a>
  79. </li>
  80. <li>
  81. <a href="#">South Goa</a>
  82. </li>
  83. </ul>
  84. </li>
  85. </ul>
  86. </div>
  87. </body>
  88. </html>

Output

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

  1. <!DOCTYPE html>
  2. <head runat="server">
  3. <title></title>
  4. <link href="http://code.jquery.com/ui/1.10.4/themes/hot-sneaks/jquery-ui.css" rel="stylesheet">
  5. <script src="http://code.jquery.com/jquery-1.10.2.js">
  6. </script>
  7. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js">
  8. </script>
  9. <script>
  10. $(document).ready(function() {
  11. $("#menu").menu({
  12. icons: {
  13. submenu: "ui-icon-transferthick-e-w"
  14. },
  15. items: "> :not(.ui-widget-header)"
  16. });
  17. });
  18. </script>
  19. <style>
  20. .ui-menu {
  21. width: 180px;
  22. margin-left: 400px;
  23. font-size: 24px;
  24. margin-top: 50px
  25. }
  26. #div1 {
  27. background-color: coral;
  28. color: ButtonHighlight;
  29. font-size: 30px;
  30. height: 450px;
  31. width: 1000px;
  32. text-align: center;
  33. margin-left: 150px
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="div1">
  39. <span>************Menu*************</span>
  40. <ul id="menu">
  41. <li class="ui-widget-header">East India</li>
  42. <li>
  43. <a href="#">Bihar</a>
  44. </li>
  45. <li>
  46. <a href="#">Assam</a>
  47. <ul>
  48. <li>
  49. <a href="#">Dhubri</a>
  50. </li>
  51. <li>
  52. <a href="#">Goalpara</a>
  53. </li>
  54. <li>
  55. <a href="#">Golaghat</a>
  56. </li>
  57. </ul>
  58. </li>
  59. <li class="ui-widget-header">North India</li>
  60. <li>
  61. <a href="#">Haryana</a>
  62. <ul>
  63. <li>
  64. <a href="#">Bhiwani</a>
  65. </li>
  66. <li>
  67. <a href="#">Gurgaon</a>
  68. </li>
  69. <li>
  70. <a href="#">Hisar</a>
  71. </li>
  72. </ul>
  73. </li>
  74. <li class='ui-state-disabled'>
  75. <a href="#">Rajasthan</a>
  76. </li>
  77. <li class="ui-widget-header">South India</li>
  78. <li>
  79. <a href="#">Goa</a>
  80. <ul>
  81. <li>
  82. <a href="#">North Goa</a>
  83. </li>
  84. <li>
  85. <a href="#">South Goa</a>
  86. </li>
  87. </ul>
  88. </li>
  89. <li class="ui-widget-header">West Indian</li>
  90. <li>
  91. <a href="#">Gujarat</a>
  92. </li>
  93. </ul>
  94. </div>
  95. </body>
  96. </html>

Output

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.