tab in Bootstrap 1) are composed of:
The trigger element (where you need to click on) where:
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link" id="home-tab" data-bs-target="#home" type="button" role="tab"
aria-controls="home" aria-selected="false">Home
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link active" id="profile-tab" data-bs-target="#profile"
type="button" role="tab" aria-controls="profile" aria-selected="true">Profile
</button>
</li>
</ul>
The panel element that contains the content where:
<div class="tab-content">
<div class="tab-pane" id="home" role="tabpanel" aria-labelledby="home-tab">Home</div>
<div class="tab-pane active" id="profile" role="tabpanel" aria-labelledby="profile-tab">Profile</div>
</div>
The result is below and you can see that the tabs are not responsive. If you click on the other nothing happens because the tab Javascript was not yet instantiated.
To make the tabs responsive when you click on a trigger element, you need to instantiate a Javascript Bootstrap Tabs object.
Bootstrap gives two ways:
By adding:
bootstrap will automatically add to this element a click callback
Demo: We have added to the triggers element, the data-bs-toggle=“tab” attribute
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab"
aria-controls="home" aria-selected="false">Home
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link active" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile"
type="button" role="tab" aria-controls="profile" aria-selected="true">Profile
</button>
</li>
</ul>
The below code:
document.querySelectorAll('#myTab button').forEach(function (triggerEl) {
triggerEl.addEventListener('click', function (event) {
let tabTrigger = bootstrap.Tab.getInstance(triggerEl);
if (tabTrigger == null) {
tabTrigger = new bootstrap.Tab(triggerEl)
}
tabTrigger.show()
});
});