I am currently working on a dropdown menu where I want to set an active state upon a click. The active class is added successfully when a link is clicked, but the issue arises when trying to remove the active class when another link is clicked. I only want one link to be active at a time.
https://i.sstatic.net/lR6gY.png
Jquery:
$(document).ready(function () {
$(document).click(function () {
$('.link.activeLink').removeClass('activeLink')
});
$('.link').click(function (e) {
$(this).toggleClass('activeLink');
if ($(e.parentNode).hasClass('show')) {
$('.activeLink').removeClass('activeLink');
}
});
});
html:
<li class="nav-item dropdown px-2 arrowDown">
<a class="nav-link link dropdown-toggle text-white paddingRightButton h-100 noPaddingRightLeft" href="#" data-toggle="dropdown" id="flatRoofingDropdown" role="button" aria-haspopup="true" aria-expanded="false">
Flat Roofing
</a>
Updated screenshot:
https://i.sstatic.net/nCRgu.png
After the dropdown closes, the link remains active, which is not the desired behavior.