Button to Trigger Nav-Tab with Twitter Bootstrap
Asked Answered
K

5

12

This button triggers the next tab to load content, but the tab itself does not switch, it remains on the first tab..

<br><a class="btn btn-primary" href="#tab2" data-toggle="tab">Review</a><br>

Here is the code for nav nav-tabs:

  <ul class="nav nav-tabs">
    <li class="active"><a href="#tab1" data-toggle="tab">Shipping</a></li>
    <li><a href="#tab2" data-toggle="tab">Quantities</a></li>
    <li><a href="#tab3" data-toggle="tab">Summary</a></li>
  </ul>

ANY SOLUTION for switching both the tab&content IS APPRECIATED

..perhaps a way to change the button to trigger the next() function in * bootstrap-tab.js v2.3.1:

  , activate: function ( element, container, callback) {
      var $active = container.find('> .active')
        , transition = callback
            && $.support.transition
            && $active.hasClass('fade')

      function next() {
        $active
          .removeClass('active')
          .find('> .dropdown-menu > .active')
          .removeClass('active')

        element.addClass('active')

        if (transition) {
          element[0].offsetWidth // reflow for transition
          element.addClass('in')
        } else {
          element.removeClass('fade')
        }

        if ( element.parent('.dropdown-menu') ) {
          element.closest('li.dropdown').addClass('active')
        }

        callback && callback()
      }

      transition ?
        $active.one($.support.transition.end, next) :
        next()

      $active.removeClass('in')
    }
  }
Keratose answered 9/5, 2013 at 13:17 Comment(0)
P
34

You could assign your Review button a click handler using jQuery...

JS:

$('#btnReview').click(function(){
  $('.nav-tabs > .active').next('li').find('a').trigger('click');
});

HTML:

<a class="btn btn-primary" href="#" id="btnReview">Review</a>

Working Demo

Philippic answered 9/5, 2013 at 13:50 Comment(1)
This is not an elegant markup-only solution. Definitely needs a fix in bootstrap.Ava
A
10

By changing 'data-toggle' to 'data-trigger' for the triggering link, the following code can trigger any tab with the same HREF attribute, for all triggers with similar markup.

(N.B. Selectors are not optimised, it's just a guide to a more flexible solution)

JS:

$( '[data-trigger="tab"]' ).click( function( e ) {
    var href = $( this ).attr( 'href' );
    e.preventDefault();
    $( '[data-toggle="tab"][href="' + href + '"]' ).trigger( 'click' );
} );

HTML:

<a class="btn btn-primary" href="#tab2" data-trigger="tab">Review</a>
Ava answered 3/9, 2014 at 10:18 Comment(0)
J
1

Using Bootstrap 4

var nextTab;    
    $('.nav-link').map(function(element) {
                    if($(this).hasClass("active")) {
                        nextTab = $(this).parent().next('li');
                    }
                })

    nextTab.find('a').trigger('click');
Jeweller answered 5/9, 2018 at 16:58 Comment(0)
D
1

It's quite simple with Bootstrap 4 & JQuery, i'm using this solution:

 $(document).ready(function(){
        activaTab('aaa');
    });

    function activaTab(tab){
        $('.tab-pane a[href="#' + tab + '"]').tab('show');
    };
Dipietro answered 14/7, 2019 at 16:30 Comment(1)
.tab('show'); did the magic. Thanks man!Introgression
R
0

You can with jquery;

JS:

function change_tab(tab_id){

   $(".nav-link").attr("class","nav-link");//clear classes
   $(".tab-pane").attr("class","tab-pane fade");//clear classes

   var tab_content = "#"+tab_id;
   var tab = "#"+tab_id+"-tab";
   $(tab).attr("class","nav-link active show");
   $(tab_content).attr("class"," tab-pane fade active show");
}

HTML :

<a class="btn btn-primary" onclick="change_tab('tabid')">Review</a>
Rehnberg answered 8/9, 2021 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.