bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

active tab does not update with internal link

Open AegirisSystems opened this issue 14 years ago • 4 comments

I had some links that directly relate to other tab content on the page, so when I set a link and add data-toggle="tab" to it, it switches tabs but does not activate a new tab.

Here is a jsfiddle example of the problem.

http://jsfiddle.net/epiceon/M6NQJ/1/

AegirisSystems avatar May 02 '12 02:05 AegirisSystems

I believe I found a solution provided by Chords - Stackoverflow

$('.btn').click(function (e) {
    $('a[href=#test1]').tab('show');
});

There is a working example provided by chords http://jsfiddle.net/gahk9/

Another solution is A little more involved, however.

$("#showtab1").click(function() {
    if ($('#test3button').hasClass('active'))
            $('#test3button').removeClass('active');
        $('#test1').addClass('active');
        $('#test1button').addClass('active');            
    return false;
});​

By Deekay - Stackoverflow

Working example http://jsfiddle.net/WnQvF/8/

​

AegirisSystems avatar May 04 '12 17:05 AegirisSystems

oh yeah - unfortunately that's a limitation of the current tab implementation. An easier workaround would be to just call click manually on the tab button you want to tab to:

   $(button).on('click', $(firstTab).click)

fat avatar Jun 03 '12 02:06 fat

This one took me a second to figure out and I wanted to make it relatively easy to scale. Here's some code and a working fiddle.

$("a[data-tab-destination]").on('click', function() {
    var tab = $(this).attr('data-tab-destination');
    $("#"+tab).click();
});

http://jsfiddle.net/technotarek/3hJ46/

technotarek avatar Feb 28 '13 18:02 technotarek

I like to generalize it more using a simple class solution

Here is a generalized example which will work with nav-pills and nav-tabs Allows any link to also control the tabs for that "on-page" navigation.

HTML

<a href="#aaa" class="control-tabs">Another Link a</a> 
<a href="#bbb" class="control-tabs">Another Link b</a>
<ul class="nav nav-tabs">
    <li><a href="#aaa" data-toggle="tab">AAA</a></li>
    <li><a href="#bbb" data-toggle="tab">BBB</a></li>
    <li><a href="#ccc" data-toggle="tab">CCC</a></li>
</ul>
<div class="tab-content" id="tabs">
    <div class="tab-pane" id="aaa">...Content...</div>
    <div class="tab-pane" id="bbb">...Content...</div>
    <div class="tab-pane" id="ccc">...Content...</div>
</div>

JS

        // other links to control tabs
        $(".control-tabs").click(function(){
            var url = $(this).attr('href');
            if (url.match('#')) {
                var tid = url.split('#')[1];
                console.log(tid);
                $('.nav a[href=#'+tid+']').tab('show') ;
                window.scrollTo(0, 0);
            }
        });

I would also suggest this for Page Reload to current tab...

        // Javascript to enable link to tab
        var url = document.location.toString();
        if (url.match('#')) {
            $('.nav a[href=#'+url.split('#')[1]+']').tab('show') ;
            window.scrollTo(0, 0);
        }

        // Change hash for page-reload
        $('.nav a').on('shown.bs.tab', function (e) {
            window.location.hash = e.target.hash;
        })

Artistan avatar Feb 19 '14 18:02 Artistan