Implement a navigational system that allows for expanding sub menus through the use

I have been developing a navigation menu that includes nested sub menus. I am interested in making the menus expandable upon clicking the parent menu item.

Below is my HTML code snippet showcasing 2 ng-repeat functions within the parent menu item, as well as an additional one for any sub-menu items containing further sub-menus.

<div id="sidebar-wrapper" ng-controller="navigationController" style="overflow-y: auto; margin-top:-20px;" class="hidden-print" ng-hide="hideSidebar()">
  <nav role="navigation" class="navbar navbar-default navbar-bigfont" style="border-width: 0">
    <ul class="nav nav-pills nav-stacked">
      <li class="portal-text-19b" ng-repeat="item in newMenu" ng-class="{active: hightlight === item.title && item.path !== '#'}" ng-show="showItem(item.path, item.allowAll)">
        <a style="text-transform: uppercase" href="{{item.path}}" ng-click="item.fn()">
          <span class="{{item.icon}}"></span>&nbsp; {{item.title}}
          <span style="font-size: 7px;" ng-if="item.childs" class="glyphicon glyphicon-play"></span>
        </a>
        <ul class="nav nav-pills nav-stacked">
          <li style="list-style-type: none;" ng-repeat="child in item.childs" ng-class="{active: hightlight === child.title && child.path !== '#'}" ng-show="showItem(child.path, child.allowAll)">
            <a href="{{child.path}}" ng-click="child.fn()">
              <span class="{{child.icon}}"></span>&nbsp; {{child.title}}
            </a>
            <ul class="nav nav-pills nav-stacked">
              <li style="list-style-type: none;" ng-repeat="child2 in child.childs" ng-class="{active: hightlight === child2.title && child2.path !== '#'}" ng-show="showItem(child2.path, child2.allowAll)">
                <a href="{{child2.path}}" ng-click="child2.fn()">
                  <span class="{{child2.icon}}"></span>&nbsp; {{child2.title}}
                </a>
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </nav>
</div>

Answer №1

To hide the inner ul elements, you can set them to be hidden and then use the code below to toggle their visibility:

$("ul li").has("ul").click(function() {
  $(this).find("ul").toggle();
});

This code will show or hide the child ul elements when their parent li is clicked.

How it works:

The code utilizes the .has() method to target only li elements that contain ul children. It then uses the .toggle() method to toggle the display of the child list within the clicked li.

See it in action:

Check out this live Demo:

$("ul li").has("ul").click(function() {
  $(this).find("ul").toggle();
});
ul li {
  cursor: pointer;
}

ul li ul {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>list item 1</li>
  <li>list item 2
    <ul>
      <li>list item 2-a</li>
      <li>list item 2-b</li>
    </ul>
  </li>
  <li>list item 3</li>
  <li>list item 4</li>
</ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to retrieve information from an array of objects and store them in individual variables?

I'm struggling to extract the necessary information from the array below, which was received from the backend. I need to utilize these values as error messages but my attempts to map have been unsuccessful. const errorArray = [ { "candidate ...

Activate the date-picker view when the custom button is clicked

Utilizing this library for date-picker functionality has been quite beneficial. I am currently working on a feature that involves opening the date-picker upon clicking a custom button. The default input is functioning properly. <input name="pickerFromD ...

In Angular and Sublime Text 2, I'm having an issue where one file is working perfectly while the other one isn't. I've closely examined both

As I follow along with the angularjs tutorial, I am attempting to replicate each step. In the section on routes, we are instructed to create a file called app.js. I am using SublimeText2 as my text editor, with both syntax settings set to javascript. Surpr ...

Enhancing Yii2 Navbar with Icons

I recently started working with Yii2 and I am in the process of creating a simple navigation bar using the built-in widget. The main challenge I am facing is how to include icons next to the menu options. The current state of my navbar is as follows: Na ...

What is the process for declaring a member variable as an extended type in TypeScript?

Is it possible to declare a "member variable" as an "extension object" rather than a static type (without relying on an interface)? Imagine something like this pseudocode: class Foo { bar -> extends Rectangle; constructor(barInstance:IRec ...

bulk purchase discount with HTML/JavaScript/PHP

I am looking to add a slider feature to my "Prices" page in order to provide customers with an instant quote based on the quantity they select. The slider should range from 1 to 500 or even up to 1000, but having an input box for customers to enter the qu ...

Is there a way to align input fields alongside the center of their corresponding labels?

Let me illustrate the issue I am encountering: https://jsfiddle.net/v10un11s/1/ Here is the snippet of my HTML code: <div> <div class="component"> <div> <label for="test1" id="short-label">Short label: </label&g ...

Guide to Using JavaScript to Update User via API in ProcessMaker

I'm attempting to use jQuery Ajax to update processmaker Users via API, but I am unsure how to send data using jQuery Ajax. I have reviewed the wiki but could not find any sample code. Can you provide me with an example? This is the data I have: { us ...

Manipulate the 'i' variable within a for loop using JavaScript, JQuery, and JSON to modify an attribute

I have 4 picture locations saved in a JSON file named img0, img1, img2, and img3. By using an AJAX call (getJSON), I retrieve the data from the file and then assign them to the "src" attribute of the images on my webpage. Everything works fine with this c ...

Encountering a "Module Not Found" Error when attempting to execute a basic "Hello World" script

I attempted to run myfirst.js program from the terminal using the specified directory and command. I expected a window to open up or a browser popup. Upon typing node myfirst.js in the terminal within the same directory, an error message was displayed: no ...

Is there a method to trigger the DOM style change hook without using asynchronous code?

We have learned that css animation/transition does not work with display: block&display: none; therefore, I attempted to use someDom.style.transition = '0.3s' someDom.style.display='block' someDom.style.opacity=1 to create an anim ...

Enhance Your Website by Integrating Slick Slider with Bootstrap 4

I am currently facing a significant issue. I am attempting to integrate three div elements into my slider, and all of them maintain a Bootstrap 4 structure. The code looks like this, and it is being generated from another Jquery function where I am inserti ...

Ensuring a URL is W3C compliant and functions properly in an Ajax request

I have a general function that retrieves URLs. This particular function is part of a plugin and returns URLs to various resources such as images and stylesheets within the plugin. GET parameters are used in these URLs for proper functionality. To ensure ...

Change the position of an HTML image when the window size is adjusted

My web page features a striking design with a white background and a tilted black line cutting across. The main attraction is an image of a red ball that I want to stay perfectly aligned with the line as the window is resized, just like in the provided gif ...

Find particular CSV data by using jQuery AJAX

Is it possible to utilize jQuery ajax for retrieving specific values from a CSV file when needed, like when a button is clicked? Imagine a CSV with the following data: country, capital, population, currency, france, paris, 65950000, euro, italy, rome, 59 ...

error: local server did not return any data

I am currently using PHP on a Linux machine. In my HTML code, I have set up an AJAX request to the local Apache server (check http://localhost), with the intention of displaying the data from the server on the screen. However, for some reason, nothing is b ...

Using a Laravel foreach loop to display data with two different properties

I am seeking assistance with my foreach loop in the view. Controller.php $expenses = Expense::groupBy('category_id')->selectRaw('sum(amount) as sum, category_id')->pluck('sum','category_id'); $categories = C ...

How can I modify the color in vue-google-chart when a filter option is selected?

I created a stack column chart with a filter that changes the color to blue when selected. Here is my Vue app: https://codesandbox.io/s/vue-dashboard-chart-6lvx4?file=/src/components/Dashboard.vue I expected the selected color to match the color in the ...

Is it possible to utilize viewport height as a trigger for classes in Gatsby?

Unique Case Working on a Gatsby site with Tailwind CSS has brought to light an interesting challenge regarding different types of content. While the blog pages fill the entire viewport and offer scrolling options for overflowing content, other pages with ...

Having trouble adjusting the top menu on my Shopify theme to fit on a single line

Check out this online store: On the site, you'll notice the menu at the top with options like Home and Chain Saw Safety. Despite my best efforts to adjust the CSS, I can't seem to get it all to fit on one line. I've played around with the ...