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
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.
At Twitter, Bootstrap has quickly become one of our many go-to front-end tools when starting new applications and sites.
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.
There's no difference. Twitter Bootstrap was the official name for version 1.0 (Twitter Bootstrap). Later the name has been shortened.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With