diff --git a/docs/javascript.html b/docs/javascript.html
index e1d679d3f4..ad4cb55dd3 100644
--- a/docs/javascript.html
+++ b/docs/javascript.html
@@ -357,6 +357,27 @@ $('#my-modal').bind('hidden', function () {
})
</script>
+ Events
+
+
+
+ | Event |
+ Description |
+
+
+
+
+ | change |
+ This event fires on tab change. Use event.target and event.relatedTarget to target the active tab and the previous active tab respectively. |
+
+
+
+
+
+$('#.tabs').bind('change', function (e) {
+ e.target // activated tab
+ e.relatedTarget // previous tab
+})
Demo
- Home
diff --git a/js/bootstrap-tabs.js b/js/bootstrap-tabs.js
index dece95be66..e0286a364a 100644
--- a/js/bootstrap-tabs.js
+++ b/js/bootstrap-tabs.js
@@ -27,21 +27,27 @@
function tab( e ) {
var $this = $(this)
- , href = $this.attr('href')
, $ul = $this.closest('ul')
- , $controlled
+ , href = $this.attr('href')
+ , previous
if (/^#\w+/.test(href)) {
e.preventDefault()
- if ($this.hasClass('active')) {
+ if ($this.parent('li').hasClass('active')) {
return
}
+ previous = $ul.find('.active a')[0]
$href = $(href)
activate($this.parent('li'), $ul)
activate($href, $href.parent())
+
+ $this.trigger({
+ type: 'change'
+ , relatedTarget: previous
+ })
}
}
@@ -59,4 +65,4 @@
$('body').tabs('ul[data-tabs] li > a, ul[data-pills] > li > a')
})
-}( window.jQuery || window.ender );
\ No newline at end of file
+}( window.jQuery || window.ender );
diff --git a/js/tests/unit/bootstrap-tabs.js b/js/tests/unit/bootstrap-tabs.js
index 2ee6761eda..1d024ecbb0 100644
--- a/js/tests/unit/bootstrap-tabs.js
+++ b/js/tests/unit/bootstrap-tabs.js
@@ -11,39 +11,67 @@ $(function () {
})
test("should activate element by tab id", function () {
- var tabsHTML = ''
+ var $tabsHTML = $(''
+ + '
')
$('').appendTo("#qunit-runoff")
- $(tabsHTML).tabs().find('a').last().click()
+ $tabsHTML.tabs().find('a').last().click()
equals($("#qunit-runoff").find('.active').attr('id'), "profile")
- $(tabsHTML).tabs().find('a').first().click()
+ $tabsHTML.tabs().find('a').first().click()
equals($("#qunit-runoff").find('.active').attr('id'), "home")
$("#qunit-runoff").empty()
})
test("should activate element by pill id", function () {
- var pillsHTML = ''
+ var $pillsHTML = $(''
+ + '
')
$('').appendTo("#qunit-runoff")
- $(pillsHTML).pills().find('a').last().click()
+ $pillsHTML.pills().find('a').last().click()
equals($("#qunit-runoff").find('.active').attr('id'), "profile")
- $(pillsHTML).pills().find('a').first().click()
+ $pillsHTML.pills().find('a').first().click()
equals($("#qunit-runoff").find('.active').attr('id'), "home")
$("#qunit-runoff").empty()
})
+ test( "should trigger change event on activate", function () {
+ var $tabsHTML = $('')
+ , $target
+ , count = 0
+ , relatedTarget
+ , target
+
+ $tabsHTML
+ .tabs()
+ .bind( "change", function (e) {
+ target = e.target
+ relatedTarget = e.relatedTarget
+ count++
+ })
+
+ $target = $tabsHTML
+ .find('a')
+ .last()
+ .click()
+
+ equals(relatedTarget, $tabsHTML.find('a').first()[0])
+ equals(target, $target[0])
+ equals(count, 1)
+ })
+
})
\ No newline at end of file