Is it possible to create a tree view navigation using javascript and css?

I have almost finished styling a tree view menu using CSS. However, I am facing an issue where the tree only expands on mouseover. I want to be able to open and close the tree by clicking on the link.

Here is my HTML:

<div id="global-nav">
        <ul>
            <li><a href="#">One</a></li>
            <li><a href="#">Two</a></li>
            <li><a href="#">Three</a></li>
            <li><a href='#' class="sidemenu-sub-menu-header">Four</a>
            <ul>
                <li><a href="#">1.2 One</a></li>
                <li><a href="#">1.2 two</a></li>
            </ul></li>
        </ul>
</div>

My CSS:

.sidemenu-sub-menu-header { font-weight: bold; color: black;}


#global-nav {font-size: 17px; }
#global-nav ul li{padding: 2px 10px 2px 10px;}
#global-nav ul ul li{ display: none;}
#global-nav li:hover ul li { display: block; }

I just need a simple script that can toggle open and close the menu. Any help is appreciated. Thanks!

Answer №1

Here is a solution for you:

$('#global-nav li a').click(function(){

   var nextUl = $(this).next('ul');

   if(nextUl[0] != undefined){
     nextUl.toggle(); //Switch the visibility on and off
   };

});

Answer №2

If you're looking to achieve your desired outcome easily, consider using this jquery plugin:

Check out the demo here:

Answer №3

CSS unable to handle click events efficiently. It is recommended to utilize the jQuery toggle function instead.

Answer №4

Explore the functionality of JavaScript's popular and commonly utilized onclick function...!

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 process of linking a PHP file to an HTML form?

Having trouble sending emails with form data since the mailto function isn't working properly. For some reason, the php file is not connecting to the html form. When I try running index.html locally and hit submit, the browser displays php code inste ...

When the user clicks, open the link in a new tab

How can I use jQuery to open a designated URL in a new tab when a user clicks anywhere on the page, but only once? I'm struggling to write the script for this and would love to hear your solutions! ...

Learn how to efficiently pass multiple props using a loop in Vue

I am dealing with an object that has multiple properties. Typically, I know which props I want to pass to my component and do it like this: <component :prop1="object.prop1" :prop2="object.prop2" :prop3="object.prop3" /> However, I want to pass the ...

Tips for updating page content without needing to refresh the page

Have you noticed on Foursquare's website: They have new feeds appearing automatically without the need to refresh the page. I'm working on a backend system to display user specific feeds. How can I achieve the same effect as Foursquare? ...

How to retrieve textfield value using Material UI and ReactJS

Just delved into the world of React and I'm struggling to figure out how to retrieve the value inputted in my textfield when the submit button is clicked. I've been referencing the examples provided here: but unfortunately, they don't addre ...

How can I deactivate the main color of the FormLabel when the focus is on the radio button group?

Is there a way to change the color of FormLabel to black instead of the primary color when the radio button group is focused? https://i.sstatic.net/h3hML.png const styles = { formLabel: { color: "#000" }, formLabelFocused: { color: "#000" ...

Troubleshooting a mysterious anomaly with a jQuery UI button

Would like to achieve something similar to this: http://jqueryui.com/demos/button/#icons When trying to replicate it, http://jsfiddle.net/p5PzU/1/ Why is the height so small? I expected a square shape but am getting a rectangle instead. What could be c ...

Guide: Playing an audio file stored on your device using Tauri

I'm currently developing an mp3 (audio) player using Tauri and Vue.js. Despite trying various solutions, none of them seem to work for me. The player is implemented with a simple video tag : <video ref="video" :key="queryUrl" ...

What is the best way to develop a card stack swiper similar to Tinder using React?

After experimenting with various packages, I found that none were satisfactory for creating a customizable card swiper. As a result, I am now considering developing my own solution. What would be the best approach for adding animations, enabling draggable ...

Utilizing JQuery to populate DIVs with JSON information

Hey! I recently received some helpful advice from a coding expert, and now I'm in the process of restructuring my code based on these recommendations: When using $.each(), you have the option to return true or false. If you return false, the loop wi ...

Shifting a div to the bottom of the window screen: A step-by-step guide

Is there a way to keep a div consistently anchored to the bottom of a browser window, similar to the chat feature on Facebook? I want it to stay at the bottom even when scrolling. Has this been fixed yet? I know IE7 is notorious for having issues with it. ...

What is the best way to transfer information between Ajax and a RestController in a Spring Boot application?

I am looking to utilize Ajax to send data to a RestController using the POST method for processing, and then return the resulting list back to Ajax. Controller @Controller public class AjaxController { @GetMapping("/web") public String web() ...

Using AngularJS to dynamically populate a dropdown menu from a JSON object

I am a beginner to Angular and currently facing my first significant challenge. The JSON object below is the address data retrieved from a third-party API and added to $scope.AddressData in a controller: $scope.AddressData = [{ "Address1":"South Row", ...

Encountering cross-domain error when attempting Google Maps API GET request

I have been working on a JavaScript function that utilizes the Google Maps places API to retrieve information. Here is the function I have written: function makeCall(url) { var data = $.ajax({ type: "GET", url: url, async: f ...

Removing a hyperlink and adding a class to a unordered list generated in JavaScript - here's how!

In my JavaScript code, I have the following implementation: init: function() { var html = []; $.each(levels, function(nr) { html.push('<li><a href="#">'); html.push(nr+1); ...

Access to Orders on Shopify has been denied due to a GraphQL error

When trying to fetch orders with '@shopify/koa-shopify-auth', I encountered an error. [GraphQL error: access denied] The query I used is as follows: query { orders(first:2) { edges { node { id name } } } ...

Implementing Asynchronous Custom Validators in Angular 4

I've encountered the following code snippet: HTML: <div [class]="new_workflow_row_class" id="new_workflow_row"> <div class="col-sm-6"> <label class="checkmark-container" i18n>New Workflow <input type="che ...

Using Chrome in Application Mode with Shortcuts Disabled

When trying to utilize Chrome in application mode as a host for an HTML5 application, it becomes evident that there are significant limitations. For instance, pressing CTRL+T launches a new Chrome browser window, allowing the user to input text into the a ...

The "next" button on my carousel is unresponsive and the automatic transitioning feature is not working

I'm having trouble implementing a carousel on my website. The next/previous buttons and automatic slides are not functioning properly. <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <!- ...

What could be the reason behind the issue of my HTML draggable feature not functioning properly on touch

I've set up a draggable div with headers and p tags, but I'm encountering an issue when trying to run it on a touch screen. The div tag isn't draggable in this scenario. Can anyone suggest the best solution for making this page touch screen ...