Using Twitter Bootstrap’s bootstrap-tab.js, I have:
<ul class="tabnavcenter" id="myTab">
<li class="active"><a href="#home" data-toggle="tab">about</a></li>
<li><a href="#tab2" data-toggle="tab">education</a></li>
<li><a href="#tab3" data-toggle="tab">experience</a></li>
<li><a href="#tab4" data-toggle="tab">verified skills</a></li>
<li><a href="#tab5" data-toggle="tab"> video</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="home">Content 1</div>
<div class="tab-pane" id="profile">...</div>
<div class="tab-pane" id="messages">...</div>
<div class="tab-pane" id="settings">...</div>
</div>
How can I get it so if I put:
<div class="tab-content">
<div class="tab-pane active" id="home">Content 2</div>
</div>
… two places in the profile (once above and once below a navbar) and with different content in each, it would work? As of now, the content appears, but once its clicked, it disappears. Can there be two “active” li’s at the same time?
Edit:
Since I’m using this in a Rails 3.2 App, I currently have the following in bootstrap-tab.js:
$('#myTab a').click(function (e) {
e.preventDefault();
$(this).tab('show');
})
$('#myTab a[href="#home"]').tab('show');
$('#myTab a[href="#tab2"]').tab('show');
$('#myTab a[href="#tab3"]').tab('show');
$('#myTab a[href="#tab4"]').tab('show');
$('#myTab a[href="#tab5"]').tab('show');
$('#myTab a[href="#home2"]').tab('show');
$('#myTab a[href="#tab22"]').tab('show');
and after putting the following in user_body.html.erb:
<script type="text/javascript">
$(function () {
$('#myTab >li>a').on('click', function (e) {
e.preventDefault();
$(this).tab('show');
//
$(this.getAttribute('href') + '2').html($(this).html());
});
});
… I get the second content in the div after refreshing the page, no change when I click on the second tab, and then a change back to the name of the first ‘a’ when I click back on the first one.
It’s a mess.
Here is one solution without extra javascript, and compatible with the plugin API.
The principle is to use 2
.tab-contentand take advantage of thedata-targetselector attribute.HTML
The first
.tab-contentcontains your normal.tab-paneand the second
.tab-contentcontains the extra.tab-panes that are optionnal – plus an empty one (#notab_elsehere)Then you have your tabs with one extra attribute,
data-target:This attribute
data-targetdefines the.tab-pane(s) associated with it. The magic is that you can use#ids or.classes or any valid jQuery selector.JavaScript
All you need to activate everything is the default code :
And you can also use your own actions to trigger the tabs as defined by the API.
You do not need that if you keep the default behavior for tabs.
EXTRA
data-toggle="tab"to theaelementsfadeclass to the.tab-pane(andfade infor the.activeone)DEMOS
Here is the demo (jsfiddle) and the demo with extra (jsfiddle)