60

I am using twitter bootstrap for a project - in particular its tab functions (http://twitter.github.com/bootstrap/javascript.html#tabs)

Now I have this tablist and when I press the tab it switches to the content of each individual tab. However, this content is preloaded in the page (the html code for the content of all tabs is already there, you only change the visibility by pressing the tabs).

However I want to only dynamically load content when a certain tab is pressed, so one will get the latest data instead of the data when the whole page was loaded.

I am planning on using jQuery for this. However, how can I make it that when a tab is pressed a certain jquery function is called?

I tried to show an alert when a tab was clicked (if that works, a jQuery function will too), but even that isn't working:

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/1.4.0/bootstrap-tabs.js"></script>
<script type="text/javascript">
$(function() {
    $('.tabs').bind('click', function (e) {
        e.target(window.alert("hello"))
    });
});
</script>

And my html:

<ul class="tabs" data-tabs="tabs">
  <li class="active"><a href="#tab1">tab 1</a></li>
  <li><a href="#tab2">tab 2</li>
</ul>

<div id="my-tab-content" class="tab-content">
  <div class="tab-pane" id="tab1">
      <h1>Tab1</h1>
      <p>orange orange orange orange orange</p>
  </div>
  <div class="tab-pane" id="tab2">
     <h1>Tab2</h1>
     <p>blue blue blue blue blue</p>
  </div>
</div>

Any idea on how to make this work?

see this for the workings of twitter bootstrap tabs: (http://twitter.github.com/bootstrap/javascript.html#tabs) and the js file it uses: http://twitter.github.com/bootstrap/1.4.0/bootstrap-tabs.js

0

7 Answers 7

73

With Twitter Bootstrap version 2 the documented way to subscribe to tab change events is

$('a[data-toggle="tab"]').on('shown', function (e) {
  e.target // activated tab
  e.relatedTarget // previous tab
})

The latest documentation on Twitter Bootstrap tab events can be found at http://getbootstrap.com/javascript/#tabs

3
  • 12
    If you want to load content into the <div> belonging to the tab, you can use $($(e.target).attr('href')) (in the event handler) to find that <div>.
    – rakensi
    Jan 24, 2013 at 16:37
  • 5
    Sometimes, its best scrolling to the bottom of the page. Thanks! Jul 29, 2013 at 14:50
  • 1
    With Bootstrap 3, you need to use 'shown.bs.tab' instead of 'shown'
    – David
    Dec 24, 2014 at 0:43
19

If you are using 2.3.2 and it's not working, try using:

$(document).on("shown", 'a[data-toggle="tab"]', function (e) {
     console.log('showing tab ' + e.target); // Active Tab
     console.log('showing tab ' + e.relatedTarget); // Previous Tab
});

2.3.2 tabs usage: http://getbootstrap.com/2.3.2/javascript.html#tabs

If you are playing with version 3 (currently RC2) then documentation shows

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
     console.log('showing tab ' + e.target); // Active Tab
     console.log('showing tab ' + e.relatedTarget); // Previous Tab
 })

RC2 Tabs Usage: http://getbootstrap.com/javascript/#tabs-usage

0
14

The bind seems to run before the DOM is ready. In addition, your selector seems to be broken and you can bind change only on select elements. You have to work around by clicking and implementing some logic. Try

$(function() {
    $('.tabs').bind('click', function (e) {
        e.target(window.alert("hello"))
    });
});

UPDATE

After consulting with the documentation, it seems that your code was only missing the DOM ready part, the rest wasn't actually a bug, which means that according to the docs the following should work:

$(function() {
    $('.tabs').bind('change', function (e) {
        // e.target is the new active tab according to docs
        // so save the reference in case it's needed later on
        window.activeTab = e.target;
        // display the alert
        alert("hello");
        // Load data etc
    });
});

What caused confusion is that the plugin overrides default selector, making #.tabs valid and also adding a change event to the tab element. Go figure why they decided this syntax was a great idea.

Anyways you can try the second sample and comment wether the alert is fired before or after the tab change.

EDIT: fixed jquery exception caused by #.tabs selector

12
  • this one works! However, it now shows the alert before it shows the new tab. How can I make it that first the new tab is shown and then the alert is displayed?
    – Javaaaa
    Nov 15, 2011 at 13:42
  • @Javaaaa it's difficult to say, please update the code in your question to reflect the current "working" state, when alert is fired before.
    – zatatatata
    Nov 15, 2011 at 13:45
  • done! I am asking because when i click a tab, a jqquery function should get some data and when it has retrieved that populate the tabs' div with that data. As it is now, it first shows the alert and only when i click ok changes the tab. Will it be the same with retrieving data, in that it will wait until the data is retrieved and only then change the tab?
    – Javaaaa
    Nov 15, 2011 at 13:48
  • @Javaaaa I added some additional information to the answer, try that and reply how it works out, according to docs, it should be OK.
    – zatatatata
    Nov 15, 2011 at 13:57
  • 1
    This is not working at all. $('#.tabs') is causing an exception in JQuery, I have tried all combinations - the only thing working is binding a click event.
    – Zorayr
    Dec 17, 2011 at 21:26
4

You can use the following snippet of code in bootstrap 3

$(document).on( 'shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
   var activatedTab = e.target; // activated tab
})
1
  • Works great except that it doesn't fire on the first tab which is already loaded on the document load? Jun 12, 2016 at 18:32
0

How about using click? Like this:

$('#.tabs').click(function (e) {
  e.target(window.alert("hello"))
})
0
0

You have an error in your syntax relating to the selector:

$('#.tabs') // should be ...
$('.tabs');

The unordered list has the class called 'tabs' in your HTML, whereas you were originally trying to select an element with the id '.tabs'.

And you'll have to take manticore's suggestion into account too. You can only use 'change' on form elements.

$(document).ready(function() {
    $('.tabs').click(function(e) {
        e.preventDefault();
        alert('tab clicked!');
    });
});
0

Documentation says: use event show.

Not the answer you're looking for? Browse other questions tagged or ask your own question.