Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter bootstrap tabs and javascript events

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

like image 240
Javaaaa Avatar asked Nov 15 '11 13:11

Javaaaa


People also ask

Can I use Bootstrap with JavaScript?

All Bootstrap's JavaScript files depend on util. js and it has to be included alongside the other JavaScript files. If you're using the compiled (or minified) bootstrap. js , there is no need to include this—it's already there.

Does Twitter still use Bootstrap?

At Twitter, Bootstrap has quickly become one of our many go-to front-end tools when starting new applications and sites.

Is Twitter Bootstrap responsive?

Responsive designWith Bootstrap 2, we've gone fully responsive. Our components are scaled according to a range of resolutions and devices to provide a consistent experience, no matter what.

What is Twitter Bootstrap vs Bootstrap?

There's no difference. Twitter Bootstrap was the official name for version 1.0 (Twitter Bootstrap). Later the name has been shortened.


2 Answers

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

like image 150
Mike Grace Avatar answered Oct 04 '22 18:10

Mike Grace


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

like image 43
Sheldon Cohen Avatar answered Oct 04 '22 18:10

Sheldon Cohen