Ensure that only one menu with a specific class is open at any given time

My goal is to ensure that both menus cannot be open simultaneously. The desired behavior is as follows: When one menu is already open and you click on another, the first one should automatically close. For a better understanding of what I am trying to achieve, please refer to my code on JSFiddle with jQuery 2.1 enabled.

Check it out here: https://jsfiddle.net/examplelink

$(document).ready(function() {
  // JavaScript logic for controlling dropdown menus
});
.main-hamburger-menu {
  /* CSS styles for hamburger menu */
}
/* DESKTOP MENU STYLES */

.hidden {
  display: none;
}
.desktop__menu {
  /* CSS selectors for desktop menu */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="main-hamburger-menu">
  <div class="desktop__menu">
    <!-- HTML structure for desktop menu -->
  </div>
</div>

<br>
<br>
<br>

<div class="main-hamburger-menu">
  <div class="desktop__menu">
    <!-- Another instance of the desktop menu HTML structure -->
  </div>
</div>

Answer №1

To achieve this functionality, you can hide all other .smaller_ul_list elements while toggling the intended one. Here is a sample code snippet:

$this.click(function(e) {
    var $smallerUlList = $(this).find('.smaller_ul_list').toggleClass(hiddenClass);
    $('.smaller_ul_list').not($smallerUlList).addClass(hiddenClass);
    e.stopPropagation();
    if ($(e.target).parent().data('desktop__menuTrigger')) {
        e.preventDefault();
    }
});

Check out the updated JSFiddle here

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

Implementing websocket functionality in Yii framework

I am currently working on integrating websocket functionality into my web application using PHP and as an extension for Yii. This will allow me to create a notification system similar to what I have in mind. My starting point is the code available at the ...

Confirm the data in each field of a form individually

I am facing an issue with a form that contains 7 fields. When I submit the form to the "register.php" page, it processes each field one by one and outputs the result as a whole. However, I need to validate the data using ajax, collect the output one by one ...

JavaScript: create a button that dynamically changes size and color when scrolling

I have a pulsing button (#scrollarrow) at the bottom of my website that disappears as I start scrolling. The jQuery code responsible for this effect is as follows: $(document).ready(function(){ $(window).scroll(function(){ if ($(thi ...

What sets apart Selenium's mouseMove() function from the physical movement of a mouse?

Imagine I have element A and element B on a webpage. Using tools like Selenium or PhantomJS, I can manipulate the mouse via coordinates. By identifying the position of element A (a link) and element B (a submit button), I can create a bezier curve or mimi ...

The attempt to test the AngularJS application using the Jasmine plugin was unsuccessful

I'm currently in the process of learning how to write test cases for my angularJS application. As a beginner, I'm searching for some sample examples of working demos. I came across an example online that uses the Jasmine plugin to test an angular ...

CoffeeScript is failing to run the code

I'm attempting to use a click function to alter the CSS code and then run a function. Here is my current code: ready: -> $("#titleDD").click -> $("#titleDD").css('text-decoration', 'underline'); $("#catDD").css( ...

Upon selecting the checkbox, the user will be redirected to an input textbox

Can I make it so that when I check a checkbox, it automatically directs me to a textbox as if I pressed the TAB key? Is there a way to achieve this using only HTML and CSS? ...

Adding the Authorization header in an Ajax request within ExtJS can be easily done by including the

I've been struggling to upload a file using ExtJS and web API. The issue I'm facing is that when trying to send an authorization header to the server, it always returns as null. I even attempted to include the header in the XHR request within the ...

Encountering undefined JavaScript functions when called from HTML

I had most of these functions working perfectly, but after restarting my program, I'm now encountering issues with undefined functions in Chrome. I can't pinpoint the exact problem, and even though I've checked through Eclipse, I still can&a ...

Unable to execute findOneAndUpdate as a function

I've been researching all morning and testing different solutions, but I'm still unable to resolve this issue. Every time I run the code below, I receive the error "TypeError: req.user.findOneAndUpdate is not a function": req.user.findOneAndUpd ...

Error: Trouble encountered when sending a request to a webservice through ajax due to an unexpected token <

$(document).ready(function () { $("#submit").click(function (e) { e.preventDefault(); var userName = $("#username").val(); var password = $("#password").val(); authenticateUser(userName, password); }); }); functio ...

Guide to invoking a server-side function through JSON data?

Here is the code I am working on: <script type="text/JavaScript> var myarray = new array(); function getsvg1() { $.ajax({ alert("hello"); type: "post", url: "WebForm1.aspx/getsvg1", ...

Reinitializing the CanvasRenderingContext2D transform with resetTransform and harnessing the power

I've encountered an unexpected issue with the Google Closure Compiler in ADVANCED mode, and I'm struggling to understand it: it's renaming the function resetTransform of CanvasRenderingContext2D Unfortunately, I can't directly share th ...

Error encountered in NEXT JS: Unable to parse URL from /api/projects or Error message: Failed to connect to 127.0.0.1:3000

Currently utilizing: export const getStaticProps = async () => { export const getStaticPaths = async () => { and accessing my API (pages/api/projects/) created with Next.js on my local host const res = await fetch("http://localhost:3000/api/ ...

Tips for connecting elements at the same index

.buttons, .weChangeColor { width: 50%; height: 100%; float: left; } .weChangeColor p { background: red; border: 1px solid; } .toggleColor { background: green; } <div class="buttons"> <p><a href="#">FirstLink</a></ ...

Can you explain the functionality of the Matrix4.multiply() method in three.js?

I am currently delving into the world of three.js and finding myself faced with a challenge. My goal is to multiply two translation matrices together in order to create a consolidated matrix that encapsulates both translations: | 1 0 0 -2 | | 1 0 0 3 | ...

Transferring a JSON object along with a function to the controller of another directive

I am facing a challenge in sending a JSON object with a function template from app.controller to another directive controller. I have chosen to pass this variable as an attribute of the inner directive's element. The issue arises when attempting to ac ...

Incorporate spacing between columns in Bootstrap 5 by adding gutters

I've been exploring ways to incorporate spacing between my columns in Bootstrap 5. I diligently followed the guidelines provided in the documentation, but I'm facing difficulties in adding gutters between my columns. Currently, I am utilizing g- ...

Having issues with AngularJS rzslider failing to dispatch updated slider value

Hello, I am currently using the rzmodule/rzslider in AngularJS. However, after changing the slider to a specific range, the ng-modal is not returning the updated value. It keeps returning the initial value of 10000 that was configured initially. $scope.sl ...

Display a standard chart using the Chart.js library, along with customizable options

I have successfully generated charts from a database using JSON script. However, I am facing an issue where the chart only displays when I click on an option value. What I want to achieve now is setting a default value option so that when the website ope ...