xxxxxxxxxx
1
<div class="dropdown">
2
<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-mdb-toggle="dropdown" aria-expanded="false">
3
Dropdown button
4
</button>
5
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
6
<li><a class="dropdown-item" href="#">Action</a></li>
7
<li><a class="dropdown-item" href="#">Another action</a></li>
8
<li><a class="dropdown-item" href="#">Something else here</a></li>
9
</ul>
10
</div>
1
1
xxxxxxxxxx
1
const url = window.location.href
2
3
document.querySelectorAll('ul.dropdown-menu li a').forEach((lank)=>lank.classList.remove('active'));
4
document.querySelectorAll(`ul.dropdown-menu li a[href="${url}"]`).forEach((lank)=>lank.classList.add('active'));
5
6
// for demo purpose
7
document.querySelectorAll(`ul.dropdown-menu li a[href="#"]`).forEach((lank)=>lank.classList.add('active'));
Console errors: 0