I want to show the drop-down links in a particular order. Right now the order is –
- Link 1
- Link 3
- Link 2
HTML Code –
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"></a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Link 1</a></li>
<li><a class="dropdown-item" href="#">Link 3</a></li>
</ul>
</li>
</ul>
Javascript Code –
if (role == "admin") {
$(".dropdown-menu").append('<li>'+
'<a class="dropdown-item" href="#">Link 2</a>'+
'</li>');
}
Can anyone tell me how can I show the links in the following order?
- Link 1
- Link 2
- Link 3
2
Answers
Try getting the
textContent
of thea
tag after append and sort it out usingtextContents.sort()
and then iterate the sortedtextContents
and update the anchor tag values back.