Submenu will reveal only when the parent is clicked on

I've been trying to find a solution to my specific issue but haven't come across one that fits exactly. I have a vertical menu with submenus, and my goal is to only display the submenu when the parent item is clicked (not hovered). Additionally, I want only one submenu to be visible at a time and for it to disappear when clicking somewhere outside of the menu. Currently, the menu is designed to work on hover, but changing it to active state has not been successful (I'm not too familiar with CSS).

/* The container */
#cssmenu > ul {
    display: block;
    position: relative;
    //width: 190px;
    width: 100%;
}

/* The list elements which contain the links */
#cssmenu > ul li {
    display: block;
    position: relative;
    margin: 0;
    padding: 0;
    width: 100%;
}

/* General link styling */
#cssmenu > ul li a {
    display: block;
    position: relative;
    margin: 2;
    width: 95%;
    height: 50px;
    background-color: #abc578;
    border-left: solid 0px #ffffff;
    border-bottom: solid 1px #ffffff;
    font: 0.7em Tahoma, sans-serif;
    font-size: 14px;
    font-weight: bold;
    color: #ffffff;
    text-decoration: none;
    padding-top: 30px;
}

/* The hover state of the menu/submenu links */
#cssmenu > ul li > a:hover,
#cssmenu > ul li:hover > a {
    color: #fff;
    text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
    background: #82c500;
    background: -webkit-linear-gradient(bottom, #82c500, #000000);
    background: -ms-linear-gradient(bottom, #82c500, #000000);
    background: -moz-linear-gradient(bottom, #82c500, #000000);
    background: -o-linear-gradient(bottom, #82c500, #000000);
    border-color: transparent;
}

/* The arrow indicating a submenu */
#cssmenu > ul .has-sub > a::after {
    content: "";
    position: absolute;
    top: 34px;
    right: 8px;
    width: 0px;
    height: 0px;

    /* Creating the arrow using borders */
    border: 4px solid transparent;
    border-left: 4px solid #d8d8d8;
}

/* The same arrow, but with a darker color, to create the shadow effect */
#cssmenu > ul .has-sub > a::before {
    content: "";
    position: absolute;
    top: 35px;
    right: 8px;
    width: 0px;
    height: 0px;

    /* Creating the arrow using borders */
    border: 4px solid transparent;
    border-left: 4px solid #000;
}

/* Changing the color of the arrow on hover */
#cssmenu > ul li > a:hover::after,
#cssmenu > ul li:hover > a::after {
    border-left: 4px solid #fff;
}

#cssmenu > ul li > a:hover::before,
#cssmenu > ul li:hover > a::before {
    border-left: 4px solid rgba(0, 0, 0, 0.3);
}

/* THE SUBMENUS */
#cssmenu > ul ul {
    position: absolute;
    left: 95%;
    width: 100%;
    top: -9999px;
    padding-left: 5px;
    opacity: 0;
    /* The fade effect, created using an opacity transition */
    -webkit-transition: opacity 0.2s ease-in;
    -moz-transition: opacity 0.2s ease-in;
    -o-transition: opacity 0.2s ease-in;
    -ms-transition: opacity 0.2s ease-in;
}

/* Showing the submenu when the user is hovering the parent link */
#cssmenu > ul li:hover > ul {
    top: -2px;
    opacity: 1;
}

If anyone has any ideas, please share!

Answer №1

After some trial and error, I found a solution to my issue by referring to this helpful resource: How do I detect a click outside an element?

Incorporating the suggested code into the footer of my website, here's what it looks like now:

 $('html').click(function() {
     hideSubMenu();
 });

 $("#cssmenu").click(function(event){
     event.stopPropagation();
 });

When I interact with the submenu, I trigger the subMenu function that displays the submenu as expected:

<div id='cssmenu'>
<ul>
    <li class='has-sub '><a href='#' onclick="subMenu('handleSubMenu1')">
    ...

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

Navigating back to the beginning of the webpage following the completion of a form submission utilizing Master Pages and Ajax

I am having an issue with my ASP.NET 4.0 page where I want to reset it to the top after a form submission so that the validation summary can be displayed properly. The setup involves using Ajax and a master page with the following simplified code: <f ...

Styling just a single div when the state changes

I have a scenario where I am rendering 4 divs based on data fetched from my backend. Each div is colored according to a state change. While I have successfully implemented this, the issue is that all 4 divs can be colored in this way simultaneously. I want ...

Hiding an element in JavaScript when the onfocus event occurs is not possible

I'm having trouble hiding a div element when clicking on a text box using JavaScript. I've tried the following code, but it's not working. Can anyone spot the error in my program and suggest a better solution? <html> <head> ...

ways to implement CSS into innerText

One of my JavaScript functions is generating error messages, and I'm looking to enhance it with some CSS. My goal is to give the error message a yellow background and red text color. //targeting elements let btn = document.getElementsByClassName("bu ...

Is there a way to modify the -webkit-overflow-scrolling style using JavaScript?

Is it possible to dynamically set -webkit-overflow-scrolling using JavaScript? I attempted the code below but it didn't work as expected $("#myDiv").css("-webkit-overflow-scrolling","none"); The "myDiv" element has the .scrollable class applied with ...

Locate the value of every selected radio button on individual forms

Currently, I have two forms with radiobuttons stored in separate div containers. I am looking to compare the selected radiobuttons from the left and right div containers. My current code checks for the selected values in both div containers, but it ends ...

Error encountered during XML parsing: root element not found. Location: Firefox Console

While I am using ASP.NET MVC, I keep encountering this error specifically in Firefox. Can anyone help me understand why this error message is popping up? What could be causing it? I am unable to pinpoint the source of this error. Does anyone have any insig ...

Continuing to reference outdated JSON data even after refreshing the page, instead of utilizing the most recent version

I have a query regarding JSON and JavaScript. I update the JSON file through a web page and save it to the server. However, when I visit the page where the information from the JSON should be displayed, it shows the old data even after refreshing. The upda ...

Panel that collapses and increments its ID each time within my loop

I have a Collapsible Panel with this header, <div id="CollapsiblePanel1" class="CollapsiblePanel"> <div class="CollapsiblePanelTab" tabindex="0">Comments</div> <div class="CollapsiblePanelContent"> Content &l ...

Generating an image in Fabric.js on a Node server following the retrieval of canvas data from a JSON

I've been trying to solve this problem for the past few days. In my app, users can upload two images, with one overlaying the other. Once they position the images and click a button, the canvas is converted to JSON format and sent to a node (express) ...

Issue with jQuery Dialog theme not getting applied

After loading the jquery-1.4.2.min.js and jquery-ui-1.8.1.custom.min.js scripts, I noticed that they both appear in the dropdown list on Firebug's Scripts tab as minimized. The dialog is launching properly, however, there seems to be an issue with the ...

Execute consecutive Angular2 functions in a sequential manner, one following the next

In my Angular2 project, I have a service that fetches data for dropdown menus on a form. However, when I call this service multiple times with different parameters in the form component initialization, only the last call seems to work, overriding the previ ...

What could be causing the image not to load in this code with the img tag?

import React from 'react'; import styled from 'styled-components'; function Navbar() { return ( <Container> <img src='./appleWhite.png' /> </Container> ) } export defau ...

How can we enforce that the input consists of digits first, followed by a space, and then

Can regex (with javascript possibly) be used to mandate numbers, followed by a space, and then letters? I'm a bit lost on where to start with this... I understand that using an HTML5 pattern would work for this... [0-9]+[ ][a-zA-Z0-9]+ However, I ...

Issue with Bootstrap 4.6 Collapse Feature Failing to Toggle (Opening or Closing)

Take a look at the code snippet below. I'm facing an issue with my Bootstrap 4.6 collapse feature not opening when I click the button. Interestingly, running $("#30-50hp").collapse('show') in the JavaScript console does make it op ...

Changes are also being made to the props value

My dialog component has a student_list props and is triggered by a watcher. Essentially, when I increase the variable dialog_watcher++ in my main component, the dialog will open. The data within the student_list props looks like this: student_list = [ ...

Send information through a form contained within a jQuery modal pop-up

This question has been brought up before, but none of the solutions provided seem to be effective for my situation. Within a jQuery UI dialog box, I have a form: <!-- Dialog: Register new user--> <div id="dialogForUser" title="Register new user" ...

Determine the End Date based on the Start Date

I currently have three input fields set up like this : Warranty Start Date : <input id="WarrantyStartDate" value="2015-11-22" /> Warranty period : <input id="WarrantyStartDate" value="24"/> //24 months// Warranty End Date : <input id="Warr ...

Using Paper Checkbox to Toggle the Visibility of a Div in Polymer

Having experience with Meteor, I typically used JQuery to toggle the display of a div along with paper-checkbox: HTML: <paper-checkbox name="remoteLocation" id="remote-chk" checked>Remote Location</paper-checkbox> <div id="autoUpdate" clas ...

Various settings in JQuery UI Slider

Does anyone know how to customize the steps in a jQuery UI slider? I want to set specific values as steps, like 0, 1200, 2000, 2200, and 3000. Any suggestions on how to achieve this? const rangeSliderInit = () => { const valueArray = [0, 400, 1000, 1 ...