jQuery UI | Menu Last Updated : 17 Dec, 2019 Comments Improve Suggest changes Like Article Like Report Menu widget in jQuery UI creates a menu that contains items and sub-items in a sub-menu related to a particular item on the menu. Menu items are created using mark-up elements using a parent-child like relation which can be created using lists and nested lists. Each menu item generally corresponds to a specific page and hence each of these elements is between an anchored tag. Syntax: $(selector, context).menu(options) You can provide one or more options at a time using a JavaScript object. If there is more than one choice to be provided then you may separate them using a comma as follows $(selector, context).menu ("action", params) For navigation, we can create a menu by using jQuery UI. We can create a main menu and sub-menu by using the menu. We will create one list and sub-list in HTML and that will be used to create the menu using jQuery UI. Width of the menu: According to your layout and style, the menu may take different width, we may require to adjust the width so we can keep this style properties inside our head tag. Example 1: html <!DOCTYPE html> <html> <head> <link href= 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/ui-lightness/jquery-ui.css' rel='stylesheet'> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <script src= "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"> </script> <style> .ui-menu { width: 8em; } </style> </head> <body> <br><br> <ul id='my_cs_menu'> <li> <div>Networking</div> </li> <li> <div>Office Mgmt</div> <ul> <li> <div>Word</div> </li> <li> <div>Excel</div> </li> <li> <div>Power Point</div> </li> <li> <div>Access</div> </li> </ul> </li> <li> <div>Programming</div> <ul> <li> <div>Backend</div> <ul> <li> <div>PHP</div> </li> <li> <div>Python</div> </li> <li> <div>JSP</div> </li> </ul> </li> <li> <div>Frontend</div> </li> </ul> </li> <li> <div>Games</div> </li> </ul> <script> $(document).ready(function() { $("#my_cs_menu").menu(); }) </script> </body> </html> Disabling Menu: We can disable a menu by using the disable option and setting its value to true. By default, the value is set as false. Example 2: html <!DOCTYPE html> <html> <head> <link href= 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/ui-lightness/jquery-ui.css' rel='stylesheet'> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <script src= "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"> </script> <style> .ui-menu { width: 8em; } </style> </head> <body> <br><br> <ul id='my_cs_menu'> <li> <div>Networking</div> </li> <li> <div>Office Mgmt</div> <ul> <li> <div>Word</div> </li> <li> <div>Excel</div> </li> <li> <div>Power Point</div> </li> <li> <div>Access</div> </li> </ul> </li> <li> <div>Programming</div> <ul> <li> <div>Backend</div> <ul> <li> <div>PHP</div> </li> <li> <div>Python</div> </li> <li> <div>JSP</div> </li> </ul> </li> <li> <div>Frontend</div> </li> </ul> </li> <li> <div>Games</div> </li> </ul> <script> $(document).ready(function() { $("#my_cs_menu").menu({ disabled: true }); }) </script> </body> </html> Icons: To point to sub-menus from the main-menu we can use icons. We can change the icons as per our choice. We can assign other values like: ui-icon-circle-triangle-e, ui-icon-arrow-1-e, ui-icon-arrowthick-1-e, ui-icon-triangle-1-e Example 3: html <!DOCTYPE html> <html> <head> <link href= 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/ui-lightness/jquery-ui.css' rel='stylesheet'> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <script src= "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"> </script> <style> .ui-menu { width: 8em; } </style> </head> <body> <ul id='my_cs_menu'> <li> <div>Networking</div> </li> <li> <div>Office Mgmt</div> <ul> <li> <div>Word</div> </li> <li> <div>Excel</div> </li> <li> <div>Power Point</div> </li> <li> <div>Access</div> </li> </ul> </li> <li> <div>Programming</div> <ul> <li> <div>Backend</div> <ul> <li> <div>PHP</div> </li> <li> <div>Python</div> </li> <li> <div>JSP</div> </li> </ul> </li> <li> <div>Frontend</div> </li> </ul> </li> <li> <div>Games</div> </li> </ul> <script> $(document).ready(function() { $("#my_cs_menu").menu({ menus: 'ul' }); }) </script> </body> </html> Menus: We can set the selector for the menu elements. By default, it is ul, we can change this value to other elements like div. Note: The output will remain same as of example 2. Example 4: html <!DOCTYPE html> <html> <head> <link href= 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/ui-lightness/jquery-ui.css' rel='stylesheet'> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <script src= "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"> </script> <style> .ui-menu { width: 8em; } </style> </head> <body> <ul id='my_cs_menu'> <li> <div>Networking</div> </li> <li> <div>Office Mgmt</div> <ul> <li> <div>Word</div> </li> <li> <div>Excel</div> </li> <li> <div>Power Point</div> </li> <li> <div>Access</div> </li> </ul> </li> <li> <div>Programming</div> <ul> <li> <div>Backend</div> <ul> <li> <div>PHP</div> </li> <li> <div>Python</div> </li> <li> <div>JSP</div> </li> </ul> </li> <li> <div>Frontend</div> </li> </ul> </li> <li> <div>Games</div> </li> </ul> <script> $(document).ready(function() { $("#my_cs_menu").menu({ menus: 'ul' }); }) </script> </body> </html> Position in Menu: We can set the position of the sub-menu relative to main menu by setting the position value. Here we have used this position value to set the position of sub-menu list. position: { my: "left bottom", at: "right top" } my: It is the tooltip box. at: The element for which the tooltip is displayed. All horizontal alignment can take three positions: left, right or center All vertical alignment can take three positions: top or bottom or center Example 5: html <!DOCTYPE html> <html> <head> <link href= 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/ui-lightness/jquery-ui.css' rel='stylesheet'> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <script src= "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"> </script> <style> .ui-menu { width: 8em; } </style> </head> <body> <br><br><br><br><br><br> <ul id='my_cs_menu'> <li> <div>Networking</div> </li> <li> <div>Office Mgmt</div> <ul> <li> <div>Word</div> </li> <li> <div>Excel</div> </li> <li> <div>Power Point</div> </li> <li> <div>Access</div> </li> </ul> </li> <li> <div>Programming</div> <ul> <li> <div>Backend</div> <ul> <li> <div>PHP</div> </li> <li> <div>Python</div> </li> <li> <div>JSP</div> </li> </ul> </li> <li> <div>Frontend</div> </li> </ul> </li> <li> <div>Games</div> </li> </ul> <script> $(document).ready(function() { $("#my_cs_menu").menu({ position: { my: "left bottom", at: "right top" } }); }) </script> </body> </html> Blur: We can Trigger any script by associating it with blur method of menu. In the below example, We will collect the name of the element from which we just moved out by using blur method. Example 6: html <!DOCTYPE html> <html> <head> <link href= 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/ui-lightness/jquery-ui.css' rel='stylesheet'> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <script src= "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"> </script> <style> .ui-menu { width: 8em; } </style> </head> <body> <br><br><br><br> <div id=display></div> <br><br> <ul id='my_cs_menu'> <li> <div>Networking</div> </li> <li> <div>Office Mgmt</div> <ul> <li> <div>Word</div> </li> <li> <div>Excel</div> </li> <li> <div>Power Point</div> </li> <li> <div>Access</div> </li> </ul> </li> <li> <div>Programming</div> <ul> <li> <div>Backend</div> <ul> <li> <div>PHP</div> </li> <li> <div>Python</div> </li> <li> <div>JSP</div> </li> </ul> </li> <li> <div>Frontend</div> </li> </ul> </li> <li> <div>Games</div> </li> </ul> <script> $(document).ready(function() { $("#my_cs_menu").menu({ blur: function(event, ui) { $('#display').html(" <b>Moved From: </b>" + ui.item.text()); } }); }) </script> </body> </html> Destroy: We can apply destroy event and remove the functionality of menu and initiate the menu again by using click event of radio button. Example 7: html <!DOCTYPE html> <html> <head> <link href= 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/ui-lightness/jquery-ui.css' rel='stylesheet'> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <script src= "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"> </script> <style> .ui-menu { width: 8em; } </style> </head> <body> <br><br><br><br> <div id=display>Display here</div> <br><br> <input type='radio' name='r_select' id='r2' value='destroy'>destroy <input type='radio' name='r_select' id='r2' value='initialize'>initialize <br> <div id=display></div> <br><br> <ul id='my_cs_menu'> <li> <div>Networking</div> </li> <li> <div>Office Mgmt</div> <ul> <li> <div>Word</div> </li> <li> <div>Excel</div> </li> <li> <div>Power Point</div> </li> <li> <div>Access</div> </li> </ul> </li> <li> <div>Programming</div> <ul> <li> <div>Backend</div> <ul> <li> <div>PHP</div> </li> <li> <div>Python</div> </li> <li> <div>JSP</div> </li> </ul> </li> <li> <div>Frontend</div> </li> </ul> </li> <li> <div>Games</div> </li> </ul> <script> $(document).ready(function() { $("#my_cs_menu").menu({ }); $("input:radio[name=r_select]").click(function() { var selection = $(this).val() if (selection == 'destroy') { $("#my_cs_menu").menu(selection); $('#display').html( " <b>destroy method invoked</b>"); } if (selection == 'initialize') { $("#my_cs_menu").menu(); $('#display').html( " <b>Menu Initialized</b>"); } }) }) </script> </body> </html> Comment More infoAdvertise with us Next Article Getting Started with jQuery M mayeshmohapatra Follow Improve Article Tags : JQuery jQuery-Misc jQuery-UI Similar Reads jQuery Tutorial jQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc 8 min read Getting Started with jQuery jQuery is a fast lightweight, and feature-rich JavaScript library that simplifies many common tasks in web development. It was created by John Resign and first released in 2006. It makes it easier to manipulate HTML documents, handle events, create animations, and interact with DOM(Document Object M 4 min read jQuery Introduction jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM). jQuery simplifies HTML document traversing and manipulation, browser event handling, DOM animations, Ajax interactions, and cross-browser Java 7 min read jQuery Syntax The jQuery syntax is essential for leveraging its full potential in your web projects. It is used to select elements in HTML and perform actions on those elements.jQuery Syntax$(selector).action()Where - $ - It the the shorthand for jQuery function.(selector) - It defines the HTML element that you w 2 min read jQuery CDN A Content Delivery Network (CDN) is a system of distributed servers that deliver web content to users based on their geographic location. Including the jQuery CDN in your project can improve the performance, enhance reliability, and simplify maintenance.What is a jQuery CDN?A jQuery CDN is a service 4 min read jQuery SelectorsJQuery SelectorsWhat is a jQuery Selector?jQuery selectors are functions that allow you to target and select HTML elements in the DOM based on element names, IDs, classes, attributes, and more, facilitating manipulation and interaction. Syntax: $(" ")Note: jQuery selector starts with the dollar sign $, and you encl 5 min read jQuery * SelectorThe jQuery * selector selects all the elements in the document, including HTML, body, and head. If the * selector is used together with another element then it selects all child elements within the element used. Syntax: $("*")Parameters: *: This parameter is used to select all elements.Example 1: Se 1 min read jQuery #id SelectorjQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. Elaborating on the terms, it simplifies HTML document traversing and manipulation, browser event handling, DOM animations, Aja 1 min read jQuery .class SelectorThe jQuery .class selector specifies the class for an element to be selected. It should not begin with a number. It gives styling to several HTML elements. Syntax: $(".class") Parameter: class: This parameter is required to specify the class of the elements to be selected.Example 1: In this example, 1 min read jQuery EventsjQuery EventsjQuery events are actions or occurrences that happen on a web page, such as clicks, hover, or keypress. jQuery provides methods to handle and respond to these events with ease. jQuery events are used to create dynamic web pages. Syntax: $(selector).method(function)Here We will explore some basic eve 4 min read jQuery bind() MethodThe jQuery bind() method is used to attach one or more event handlers for selected elements. This method specifies a function to run when an event occurs. Syntax$(selector).bind(event, data, function);ParametersIt accepts three parameters that are described below: event: This is an event type that 2 min read jQuery blur() MethodjQuery blur() is an inbuilt method that is used to remove focus from the selected element. This method starts the blur event or it can be attached a function to run when a blur event occurs. Syntax:$(selector).blur(function)Parameters: It accepts an optional parameter "function". jQuery examples t 1 min read jQuery change() MethodThe jQuery change() method is used to detect changes in the value of form elements like <input>, <select>, or <textarea>. It triggers a specified function when a user modifies the input or selects a different option.Syntax$(selector).change(function)Parameters : It accepts an optio 2 min read jQuery EffectsjQuery animate() MethodThe animate() method is an inbuilt method in jQuery which is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element. Syntax: (selector).animate({styles}, para1, para2, para3); Here "select 2 min read jQuery clearQueue() MethodThe jQuery clearQueue() method removes all items from the queue that have not yet been run. Note that when a function has started to run, it runs until it is completed. Syntax: $(selector).clearQueue(name); Here "selector" is the selected element. Parameter: It accepts a parameter "name" which is th 2 min read jQuery delay() MethodjQuery delay() method is an inbuilt that is used to set a timer to delay the execution of the next item in the queue. Syntax:$(selector).delay(para1, para2);Parameter: It accepts two parameters which are specified below: para1: It specifies the speed of the delay.para2: It is optional and specifies 2 min read jQuery HTML/CSSjQuery addClass() MethodThe addClass() method is an inbuilt method in jQuery that is used to add more properties to each selected element. It can also be used to change the property of the selected element. This method can be used in two different ways: 1) By adding a Class name directly: Here, the Class name can be used d 2 min read jQuery after() MethodThe after() method is an inbuilt function in jQuery which is used to insert content, specified by the parameter for each selected element in the set of matched elements. Syntax: $(selector).after(A);Parameter: It accepts a parameter "A" which is either a content or function passed to the method. Ret 1 min read jQuery append() MethodThe append() method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html) ) Parameters: This method accepts two parameters as mentioned above and described below: content: It is a required parameter and is used to spe 2 min read jQuery TraversingjQuery | TraversingIn jQuery, traversing means moving through or over the HTML elements to find, filter or select a particular or entire element. Based on the traversing purposes following methods are Categorized as follows: Tree Traversing: Ancestors: parent() it gives parent element of specified selector Syntax: $(s 4 min read jQuery add() methodThe jQuery add() method is used to add elements to the existing group of elements. This method can add element to the whole document, or just inside the context element if the context parameter is defined. Syntax: $(selector).add(element, context_parameter) Here selector helps to find the matching e 1 min read jQuery addBack() MethodThe addBack() is an inbuilt method in jQuery that adds the previous set of elements to the current set. This method adds previous DOM tree elements to the current set and maintains them in the internal stack which will take care of changes to the matched set of elements. Syntax: .addBack(selector) P 2 min read Like