I have multiple dropdowns within a top level dropdown menu. I have an issue where when I click to open one submenu, it remains open when I click to open another. Would like to auto close the others when another submenu is clicked open.
MY HTML:
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<div class="container-fluid"> <a class="navbar-brand" href="#"> LOGO </a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button>
<!-- start test -->
<div class="row navbar-collapse" id="navbarCollapse">
<div class="col-md-12 order-last menu-one">
<!-- start-->
<ul class="navbar-nav ms-auto mb-2 mb-md-0 nav navbar-right justify-content-end">
<hr class="white d-block d-md-none">
<li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">login </a> </li>
<li class="nav-item"> <a class="nav-link" href="#">support <span class="text-ltred">1300 000 123</span></a> </li>
<li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Global sites </a>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item" href="#">United States / Canada</a></li>
<li><a class="dropdown-item" href="#">New Zealand</a></li>
</ul>
</li>
<li class="nav-item" id="search">
<div class="search-box nav-link">
<input type="text" class="search-input" placeholder="Search">
<a class="search-btn" href="#"> ? </a> </div>
</li>
</ul>
<!-- end -->
</div>
<div class="col-md-12 order-md-last menu-two">
<!-- start-->
<ul class="navbar-nav ms-auto mb-2 mb-md-0 nav navbar-right justify-content-end">
<li class="nav-item dropdown has-megamenu"> <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Solutions & services</a>
<div class="dropdown-menu megamenu" role="menu">
<div class="row g-3">
<div class="col-md-6 col-12">
<div class="col-megamenu">
<h6 class="title">Solutions & services</h6>
<ul class="list-unstyled">
<li class="dropend"> <a href="#" class="dropdown-item dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" data-bs-auto-close="true" aria-expanded="false">Leasing solutions</a>
<ul class="dropdown-menu no-border">
<li><a class="dropdown-item" href=""> Transfer my fleet</a></li>
<li><a class="dropdown-item" href=""> Start a new fleet</a></li>
<li><a class="dropdown-item" href=""> Electric vehicles</a></li>
<li><a class="dropdown-item" href=""> Next lease</a></li>
<li><a class="dropdown-item" href=""> Trucks, plant & equipment</a></li>
<li><a class="dropdown-item" href=""> Used vehicles</a></li>
</ul>
</li>
<li class="dropend"> <a href="#" class="dropdown-item dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" data-bs-auto-close="true" aria-expanded="false">Fleet management services</a>
<ul class="dropdown-menu no-border">
<li><a class="dropdown-item" href=""> Accident management</a></li>
<li><a class="dropdown-item" href=""> Acquisition and disposals</a></li>
<li><a class="dropdown-item" href=""> Driver safety</a></li>
<li><a class="dropdown-item" href=""> Fleet card</a></li>
<li><a class="dropdown-item" href=""> Hire vehicles</a></li>
<li><a class="dropdown-item" href=""> Infringement management</a></li>
<li><a class="dropdown-item" href=""> Insurance</a></li>
<li><a class="dropdown-item" href=""> Maintenance</a></li>
<li><a class="dropdown-item" href=""> PoolCar sharing platform</a></li>
<li><a class="dropdown-item" href=""> Registration</a></li>
<li><a class="dropdown-item" href=""> Roadside assistance</a></li>
<li><a class="dropdown-item" href=""> Telematics</a></li>
<li><a class="dropdown-item" href=""> Toll management</a></li>
</ul>
</li>
<li class="dropend"> <a href="#" class="dropdown-item dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" data-bs-auto-close="true" aria-expanded="false">Fleet optimisation</a>
<ul class="dropdown-menu no-border">
<li><a class="dropdown-item" href=""> Corporate financing</a></li>
<li><a class="dropdown-item" href=""> Leasing</a></li>
<li><a class="dropdown-item" href=""> Sales and lease back</a></li>
<li><a class="dropdown-item" href=""> Strategic consulting</a></li>
<li><a class="dropdown-item" href=""> Technology integration</a></li>
</ul>
</li>
</ul>
</div>
<!-- col-megamenu.// -->
</div>
<!-- end col-3 -->
</div>
<!-- end row -->
</div>
<!-- dropdown-mega-menu.// -->
</li>
<li class="nav-item"> <a class="nav-link" href="#">Driver Support</a></li>
<li class="nav-item"> <a class="nav-link" href="#">About</a></li>
<li class="nav-item"> <a class="nav-link" href="#">News</a></li>
<li class="nav-item"> <a class="nav-link" href="#">Resources</a></li>
<li class="nav-item"> <a class="btn btn-bd-primary" href="#" role="button">Contact us</a></li>
</ul>
<!-- end -->
</div>
</div>
<!-- end test -->
</div>
</nav>
You can see the full code I have at:
https://www.codeply.com/p/YfWQ2HX73q
I am stuck, and really would like the help to get this menu working beautifully.
2
Answers
use
for
loop to iterate through the elements withclassName=dropdown-menu
and change itclassName
accordinglyeg:
Get all submenus into a list, and then add click listener, and on click, close all submenus in the list, but the one that is clicked:
working snippet: