Built a Bootstrap dropdown navbar with custom jQuery & CSS, but facing an issue with toggling submenu in Mobile devices.
See the code snippet below:
$('.dropdown').click(function() {
$(this).children('.dm-lv1').toggleClass('show');
e.preventDefault();
});
$('.dropdown-submenu').click(function() {
$(this).children('.dm-lv2').toggleClass('show');
e.preventDefault();
});
.dropdown-submenu {
position: relative;
}
.dropdown-menu {
top: 0;
left: 100%;
box-shadow: 0 0px 5px rgba(0, 0, 0, 0.4);
}
.dropdown-menu {
top: 90%;
padding: 0;
border: 0;
border-radius: 0;
border-top: 2px solid #333;
background: #eee;
}
.dropdown-item {
padding: .5rem 1rem;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav ml-auto">
<li class="nav-item active"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Products
</a>
// Dropdown menu content
</li>
<li class="nav-item active"><a class="nav-link" href="#">Clients</a></li>
<li class="nav-item active"><a class="nav-link" href="#">News</a></li>
<li class="nav-item active"><a class="nav-link" href="#">Contact US</a></li>
</ul>
</div>
</div>
</nav>
Is there a way to toggle (open/close) this dropdown and submenu on click rather than hover?