I've been looking around the net and tried many examples but can't figure out why I can't show a specific tab thru jQuery. This is my tab html:
<ul class="nav nav-tabs nav-inside-tabs" id="cartTabs">
<li class="active"><a href="#cart-tab" data-toggle="tab" class="cart-btn">Purchases</a></li>
<li><a href="#cart-data-tab" data-toggle="tab" id="cart-data-btn" class="cart-btn">Data</a></li>
<li><a href="#cart-pay-tab" data-toggle="tab" id="cart-pay-btn" class="cart-btn">Pay</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="cart-tab">
Cart
</div>
<div class="tab-pane" id="cart-data-tab">
Content data
</div>
<div class="tab-pane" id="cart-pay-tab">
Content pay
</div>
</div>
I tried a switch and the alert show up good, but doesn't activate the correct tab:
$('#cart-goon-btn').click(function(e){
e.preventDefault();
if(!$(this).attr('disabled')){
var current_tab = $('.tab-pane.active').attr('id');
switch(current_tab){
case 'cart-tab':
$('#cartTabs li a').eq($('#cart-data-tab')).tab('show');
alertify.alert('1')
break;
case 'cart-data-tab':
//cart-pay-tab
alertify.alert('2')
break;
case 'cart-pay-tab':
//checkout
break;
}
}
});
Any hint?
By using data-toggle property we can get selected bootstrap tab value in jQuery on tab change.
Creating Dynamic Tabs via Data Attributes You can activate a tab or pill navigation without writing any JavaScript code — simply specify the data-bs-toggle="tab" on each tab, or data-bs-toggle="pill" on each pill, as well as create a . tab-pane with unique ID for every tab and wrap them in .
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 . tab-content .
Tabs can be added dynamically by passing array of items and index value to the addTab method. // New tab title and content inputs are fetched and stored in local variable let title: string = document. getElementById('tab-title'). value; let content: string = document.
Instead of calling .tab('show')
on the div
you want to be visible, try doing it on the a
element that you would use to activate it:
$('a[href="#cart-data-tab"]').tab('show');
Or probably better yet, just do it by id:
$('#cart-data-btn').tab('show');
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