When a dropdown is clicked, it will close any other dropdowns that are currently

Upon clicking on the top menu link "menu item 01", the 3 level drop down opens as expected. Similarly, when you click on "menu item 04", the mega menu expands perfectly.

However, I am looking to achieve a functionality where if the user clicks on any other top menu link, all previously opened dropdown items should be hidden, leaving only one dropdown visible at a time.

To see an example, please visit this link.

 var mymenu = window.matchMedia("screen and (min-width: 781px)")
if (mymenu.matches){
$(function() {
$('.navstyle-list li a, .navstyle-submenu li a, .navstyle-submenu-sub-sub  li a').click(function(){
  $(this).next('.navstyle-submenu').toggle(300);
  $(this).next('.navstyle-submenu-sub').toggle(300);
  $(this).next('.navstyle-submenu-sub-sub').toggle(300);
  $(this).next('.megamenu').toggle(60);
});

$(document).click(function(e){
  var target = e.target;
  if (!$(target).is('.navstyle-list li a, .navstyle-submenu li a, .navstyle-submenu-sub-sub  li a') && !$(target).parents().is('.navstyle-list li a, .navstyle-submenu li a, .navstyle-submenu-sub-sub  li a')) {
    $('.navstyle-submenu, .navstyle-submenu-sub, .navstyle-submenu-sub-sub, .megamenu').hide(100);
  }
});
});
}
else{

}

Answer №1

While I don't necessarily recommend using important styling, there is potential to simplify your css significantly, avoiding the need for it altogether without having to completely rewrite all of your css.

The key solution here is to apply the open class to the list item, which will dictate whether or not the child should be displayed as open.

Additionally, there's a check in place to ensure that the open class is only removed from the list item when clicking on a new list item.

$('a').on('click', function(){
    if(!$(this).parents().hasClass('open')){
        $('li').removeClass('open');    
    }
    $(this).parent().addClass('open');
});

http://jsfiddle.net/ag46ct2u/9/

To eliminate specific styling and unnecessary repetition, you may consider reintroducing some styling to the list items. This approach would help get rid of unnecessary classes like .navstyle-submenu, .navstyle-submenu-sub, and .navstyle-submenu-sub-sub

li ul {
    display: none;
}

li.open > ul, li.open > div {
    display: block;
}

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

Issue with API delivering zip file - Headers cannot be modified after being sent to the client

I've encountered a problem with my API that generates a zip file using the archiver module and sends it back as a response to be downloaded on the client side. Whenever I attempt to send the zip file back, I receive the error Error [ERR_HTTP_HEADERS_S ...

Suggestions for efficiently filtering nested objects with multiple levels in RXJS within an Angular environment?

Just a Quick Query: Excuse me, I am new to Typescipt & RxJS. I have this JSON data: [ { "ID": "", "UEN": "", "Name": "", "Address": "", "Telephone&quo ...

The issue of the JQuery method failing to function properly on a button arises when the button is added

Upon loading a HTML page containing both HTML and JavaScript, the code is structured as shown below: <button id="test"> test button </button> <div id="result"></div> The accompanying script looks like this (with jQuery properly in ...

Tips for making a standout testimonial card?

Apologies for the simplistic inquiry. As a fresh student of a full stack web developer bootcamp, I find myself grappling with this particular challenge. How can I go about creating a testimonial card similar to this one? I've attempted to write code ...

Changing innerHTML with HTML generated by a PHP function? (utilizing xajax)

I have managed to successfully update the content inside a DIV using xajax when clicking a link. However, I noticed that it only works when I directly assign HTML within the function itself, not when calling it from another function. Allow me to demonstra ...

Using jQuery's .hover() or .animate() functions could potentially result in animated bubbles within the Marquee plugin

Exploring the capabilities of the Giva Labs jQuery Marquee plugin has been quite interesting. You can check out an example on jsFiddle here. In the example, there are two lines of text that toggle: First line and Second row. Playing around with moving the ...

What is the procedure for adding a data table to an HTML table with JSON?

I'm trying to display a table fixture layout in an HTML table using the Ajax method, but it's not working. I'm not sure what the problem is. Can someone please help me out with my code? Controller JsonResult public JsonResult FixturesVal( ...

The PhoneGap Android whitelist feature seems to be malfunctioning and preventing navigation to the next page

Currently, I am utilizing the jQuery function load() with the code snippet $('#result').load('http://.... #div'); to retrieve the content of an external website. Moreover, I have made modifications to the domain whitelist specifically f ...

Having trouble with aligning HTML elements with Bootstrap v4 framework

In the header, I have a container for the logo and another one for the login form. I want the logo to be on the left and the login form to be positioned at the top right corner. However, when I use float-left for the logo container and float-right for the ...

Interacting with PHP through JavaScript function calls

I am facing some frustration with my website due to an issue involving PHP. When I retrieve data from a PHP file, it returns a JSON list like this: {"Data":{"Recipes":{"Recipe_5":{"ID":"5","TITLE":"Spaghetti Bolognese"},"Recipe_7":{"ID":"7","TITLE":"Wurst ...

Having trouble executing the typescript build task: Command 'C:Program' is not valid as an internal or external command

I'm currently working on converting typescript code to JavaScript and have been following the steps outlined in the documentation. To automate the compilation of .ts files, I set up a watch task triggered by pressing Ctrl+Shift+B. However, upon runni ...

Translating SQL to Sequelize Syntax

I have an SQL query that I need to rewrite as a sequelize.js query in node.js. SELECT historyTable1.* FROM table1 historyTable1 WHERE NOT EXISTS ( SELECT * FROM table1 historyTable2 WHERE historyTable2.id=historyTable1.id AND historyTable2.da ...

In a React TypeScript application, prevent users from clicking on buttons that represent the same number of answers in a multiple choice question

I'm currently developing a multiple choice component with various properties like the number of options, number of answers, and a callback function to capture user-selected data. Additionally, this component should display a checkmark when a button is ...

"Utilize Angular's $http options for requesting instead of using

When I attempt to send a $http POST request to a server API, the request method unexpectedly changes to OPTIONS. Strangely, this issue only occurs when working with localhost. To troubleshoot, I tried making the same request using Postman and it worked fla ...

Steps for bringing the image to the front and changing the background color to black

I am trying to create a function where a button calls the ID image. However, I want the background div image to be unclickable and for the image to appear in front of all other divs when the page is loading. What CSS style should I add to achieve this? ...

React.memo is failing to stop unnecessary re-renders of a stateless functional child component that does not receive any props

There is a stateless functional component known as Title, which utilizes React-Reveal to showcase simple heading elements upon rendering. Despite having no internal state and not receiving any props from its parent component (GameHeader), the issue arises ...

What is the best way to quickly access the selected value from a Dropdownlist?

I am facing an issue with two drop downs that both have auto postback enabled. When I select an item from dropdown1, it sends an ID to dropdown2 and displays the corresponding data. However, when the page first loads, it does not show the selected value fo ...

Can $refs cause issues with interpolation?

I'm currently learning Vue.js and the course instructor mentioned that modifying the DOM element using $refs should not affect interpolation. In fact, any changes made directly to the DOM will be overridden by interpolation if it exists. However, in m ...

"Quotes are essential in Javastript syntax for specifying string values

I need to implement a JavaScript syntax to generate unique URLs for each image. The Robohash website provides random robot images based on different URL endings. I tried the code below, but it seems like ${props.id} is being interpreted as part of the UR ...

React Virtualized - Blank screen issue occurring because of continuous scrolling through a large list of ITSM items

I am currently working on a lengthy list of items and utilizing the react-virtualized library for this purpose. However, I have encountered an issue that needs addressing. https://i.stack.imgur.com/ROdjp.gif Upon attempting to scroll down for 2 seconds, ...