jQuery UI menu collapse() Method

jQuery UI consists of GUI widgets, visual effects, and themes implemented using jQuery, CSS, and HTML. jQuery UI is great for building UI interfaces for the webpages. jQuery UI menu is a themeable menu that is used with mouse and keyboard interactions for navigating between pages. In this article, we will use the collapse option to collapse the active sub-menu. 

Syntax:

$(".selector").menu( "collapse" );

Approach:

  • First, add jQuery UI scripts needed for your project.

<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>

Example 1: In this example, we will be using the collapse option for the menu() method. 

HTML




<!doctype html>
<html lang="en">
  
<head>
    <meta charset="utf-8">
    <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>
  
    <style>
        .ui-menu {
            width: 200px;
        }
    </style>
  
    <script>
        $(function() {
            var menu = $("#gfg").menu();
            $("#gfg").menu()
            $(menu).mouseleave(function() {
                menu.menu('collapse');
            });
        });
    </script>
</head>
  
<body>
    <h1>jQuery UI | menu collapse method</h1>
    <ul id="gfg">
        <li><a href="#">Menu</a>
            <ul>
                <li><a href="#">Submenu</a>
                    <ul>
                        <li><a href="#">
                            Sub-Submenu
                        </a></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</body>
  
</html>


Output: 

  • With collapse method.

  • Without collapse method. In the following image, please notice that the sub-sub menu is not getting collapsed as in the first image.



Contact Us