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
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
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:
What caused confusion is that the plugin overrides default selector, making
#.tabsvalid 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
#.tabsselector