Tabs help organize content in small spaces. Using JavaScript to switch tabs lets users see different content without reloading the page.
Tab navigation via JavaScript in Bootsrap
var tabTrigger = new bootstrap.Tab(document.querySelector('#myTab button[data-bs-target="#profile"]'));
tabTrigger.show();new bootstrap.Tab(element) to create a tab object for the tab button element.show() on the tab object to activate that tab and show its content.var firstTab = new bootstrap.Tab(document.querySelector('#myTab button:first-child'));
firstTab.show();var profileTab = new bootstrap.Tab(document.querySelector('#myTab button[data-bs-target="#profile"]'));
profileTab.show();document.querySelector('#myTab button[data-bs-target="#settings"]').addEventListener('click', function () { var settingsTab = new bootstrap.Tab(this); settingsTab.show(); });
This example shows three tabs: Home, Profile, and Contact. The Home tab is active by default. When you click the "Go to Profile Tab" button, JavaScript activates the Profile tab and shows its content without reloading the page.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Tab Navigation via JavaScript</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main class="container mt-4"> <h1>Tab Navigation Example</h1> <ul class="nav nav-tabs" id="myTab" role="tablist"> <li class="nav-item" role="presentation"> <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button> </li> <li class="nav-item" role="presentation"> <button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button> </li> <li class="nav-item" role="presentation"> <button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact</button> </li> </ul> <div class="tab-content p-3 border border-top-0" id="myTabContent"> <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab"> <p>Welcome to the Home tab content.</p> </div> <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab"> <p>This is the Profile tab content.</p> </div> <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab"> <p>Here is the Contact tab content.</p> </div> </div> <button id="goProfile" class="btn btn-primary mt-3">Go to Profile Tab</button> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <script> // When the button is clicked, activate the Profile tab using JavaScript document.getElementById('goProfile').addEventListener('click', () => { const profileTabTrigger = new bootstrap.Tab(document.querySelector('#myTab button[data-bs-target="#profile"]')); profileTabTrigger.show(); }); </script> </body> </html>
Make sure to include Bootstrap's JavaScript bundle for tab functionality.
Use semantic roles and aria attributes for accessibility, like role="tablist" and aria-selected.
JavaScript tab activation is useful for dynamic user interactions beyond just clicking tabs.
Tabs organize content in small spaces and improve user experience.
Use new bootstrap.Tab(element).show() to switch tabs with JavaScript.
Always include proper accessibility attributes for better keyboard and screen reader support.
Practice
new bootstrap.Tab(element).show() do in Bootstrap tab navigation?Solution
Step 1: Understand the method purpose
The methodnew bootstrap.Tab(element).show()is designed to activate a specific tab.Step 2: Effect on tab content
Callingshow()displays the content linked to the tab and marks the tab as active.Final Answer:
It activates and displays the tab content linked to the given tab element. -> Option DQuick Check:
Tab activation = show() method [OK]
- Thinking show() hides tabs
- Confusing tab creation with activation
- Assuming show() removes elements
Solution
Step 1: Recall Bootstrap tab activation syntax
The correct syntax requires creating a new Tab instance withnew bootstrap.Tab(element).Step 2: Call the show() method on the instance
After creating the instance, call.show()to activate the tab.Final Answer:
new bootstrap.Tab(element).show() -> Option CQuick Check:
Correct syntax = new + Tab + show() [OK]
- Omitting 'new' keyword
- Calling show() directly on bootstrap.Tab
- Using non-existent methods like showTab()
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-bs-toggle="tab" href="#home" role="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" id="profile-tab" data-bs-toggle="tab" href="#profile" role="tab">Profile</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="home" role="tabpanel">Home Content</div>
<div class="tab-pane fade" id="profile" role="tabpanel">Profile Content</div>
</div>
<script>
var profileTab = document.querySelector('#profile-tab');
var tab = new bootstrap.Tab(profileTab);
tab.show();
</script>Solution
Step 1: Identify the tab activated by JavaScript
The script selects the element with idprofile-taband callsshow()on it.Step 2: Understand tab content visibility
Callingshow()activates the Profile tab and displays its content, hiding the Home content.Final Answer:
Profile Content is visible -> Option BQuick Check:
tab.show() switches to Profile tab [OK]
- Assuming original active tab stays visible
- Thinking both tabs show simultaneously
- Ignoring the effect of tab.show()
var tabTrigger = document.getElementById('settings-tab');
tabTrigger.show();Solution
Step 1: Check method usage on DOM element
The DOM element returned by getElementById does not have ashow()method.Step 2: Correct way to activate tab
You must create a Bootstrap Tab instance withnew bootstrap.Tab(tabTrigger)before callingshow().Final Answer:
The element does not have a show() method directly; must create a Tab instance first. -> Option AQuick Check:
Must use new bootstrap.Tab(element) before show() [OK]
- Calling show() directly on DOM element
- Confusing querySelector with getElementById
- Assuming show() is a native DOM method
<button id="activate-settings">Go to Settings</button>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-bs-toggle="tab" href="#home" role="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" id="settings-tab" data-bs-toggle="tab" href="#settings" role="tab">Settings</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="home" role="tabpanel">Home Content</div>
<div class="tab-pane fade" id="settings" role="tabpanel">Settings Content</div>
</div>
<script>
// Which code activates the Settings tab on button click?
</script>Solution
Step 1: Identify correct event listener setup
UsingaddEventListener('click', ...)is a standard way to handle button clicks.Step 2: Properly activate the tab
Creating a newbootstrap.Tabinstance for the 'settings-tab' element and callingshow()activates the tab correctly.Final Answer:
document.getElementById('activate-settings').addEventListener('click', () => { new bootstrap.Tab(document.getElementById('settings-tab')).show(); }); -> Option AQuick Check:
Use new Tab + show() inside click listener [OK]
- Calling show() directly on DOM element
- Using non-existent bootstrap.Tab.show() method
- Using click() instead of show() to activate tab
Ready to prove it?
Pass the test and this pattern moves unknown
