Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Twitter Bootstrap Tab select

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?

like image 678
Mr.Web Avatar asked Aug 26 '13 11:08

Mr.Web


People also ask

How to select Bootstrap tab using jquery?

By using data-toggle property we can get selected bootstrap tab value in jQuery on tab change.

How to create dynamic Tabs using Bootstrap?

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 .

How to create tab menu in Bootstrap?

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 .

How to create Tabs dynamically?

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.


1 Answers

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');
like image 137
Cᴏʀʏ Avatar answered Oct 16 '22 22:10

Cᴏʀʏ