active tab does not update with internal link
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/
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;
});
Working example http://jsfiddle.net/WnQvF/8/
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)
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/
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;
})