Twitter bootstrap tabs and javascript events

JavascriptJqueryTwitter Bootstrap

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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

Solution 4 - Javascript

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
})

Solution 5 - Javascript

How about using click? Like this:

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

Solution 6 - Javascript

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!');
    });
});

Solution 7 - Javascript

Documentation says: use event show.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJavaaaaView Question on Stackoverflow
Solution 1 - JavascriptMike GraceView Answer on Stackoverflow
Solution 2 - JavascriptSheldon CohenView Answer on Stackoverflow
Solution 3 - JavascriptzatatatataView Answer on Stackoverflow
Solution 4 - JavascriptSadra AbedinzadehView Answer on Stackoverflow
Solution 5 - JavascriptManticoreView Answer on Stackoverflow
Solution 6 - JavascriptmrleeView Answer on Stackoverflow
Solution 7 - JavascriptperoksidView Answer on Stackoverflow