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
How about using click? Like this:
$('#.tabs').click(function (e) {
e.target(window.alert("hello"))
})
Documentation says: use event show
.
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
})
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
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!');
});
});
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