I’m trying to create a page navigation list-group using bootstrap.
When a user clicks on a new link in the list-group, the active status must be removed from the previous link and placed on the new link.
The problem is the the blue active link is not being removed from the previous link and this creates multiple blue active links at the same time, like you see in the picture. I would like to see only one blue active link at the same time.
Could you help me spot the problem with my code?
Thanks!
The code:
<!-- List group -->
<ul class="list-group" id="myList" role="tablist">
<li class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab">Home</li>
<li class="list-group-item list-group-item-action" data-toggle="list" href="#profile" role="tab">Profile</li>
<li class="list-group-item list-group-item-action" data-toggle="list" href="#messages" role="tab">Messages</li>
<li class="list-group-item list-group-item-action" data-toggle="list" href="#settings" role="tab">Settings</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane active" id="home" role="tabpanel">A</div>
<div class="tab-pane" id="profile" role="tabpanel">B</div>
<div class="tab-pane" id="messages" role="tabpanel">C</div>
<div class="tab-pane" id="settings" role="tabpanel">D</div>
</div>
2
Answers
So, when you fire
click
event loop through the otherli
elements and remove theactive
class, then add theactive
class the clickedli
item, same withtabs
Bootstrap has in-built
navs
that takes care of this. So there is no need to uselist-group
with custom javascript.