I have tried to change the active tab and failed to use the javascript onclick element correctly. Here's the code:
<ul class="nav nav-tabs" style="text-align: left;"> <li class="active"><a href="#first" data-toggle="tab">First</a></li> <li><a href="#second" data-toggle="tab">Second</a></li> <li><a href="#third" data-toggle="tab">Third</a></li> </ul> <div class=“tab-content"> <div id="first" class="tab-pane fade in active"> Text <a href="#second" class="btn btn-primary btn-lg" onclick="CHANGE ACTIVE TAB TO SECOND" data-toggle="tab">Second</a> <a href="#third" class="btn btn-primary btn-lg" onclick="CHANGE ACTIVE TAB TO THIRD" data-toggle="tab">Third</a> </div> <div id="second" class="tab-pane fade in"> Text </div> <div id="third" class="tab-pane fade in"> Text </div> </div>
How could I be able to use the buttons to change the active tab to their associated id? Please advise.
Have a great day!
To make the tabs toggleable, add the data-toggle="tab" attribute to each link. Then add a . tab-pane class with a unique ID for every tab and wrap them inside a <div> element with class .
You can activate a tab or pill navigation without writing any JavaScript by simply specifying data-toggle="tab" or data-toggle="pill" on an element. Adding the nav and nav-tabs classes to the tab ul will apply the Bootstrap tab styling, while adding the nav and nav-pills classes will apply pill styling.
You can always modify your navigation list through css. Use this css code instead, in that way use you use the a tag to change the color and its background-color. It's improper to use the li tag to set the background only and a tag to set the color of the text.
Just add the class to your html. This will make the first tab active by default.
you may also use .tab('show')
for more flexibility
https://codepen.io/jacobgoh101/pen/ALELNr
<button class="btn btn-primary" onclick="menu3()">go to menu 3</button>
javascript:
function menu3(){ $('[href="#menu3"]').tab('show'); }
Bootstrap documentation: https://getbootstrap.com/javascript/#tabs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With