This should be fairly simple, but somehow not working.
I am trying to change the active class of list groups upon click with the help of bootstrap tab events.
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
$(e.target).addClass('active'); // activated tab
$(e.relatedTarget).removeClass('active'); // previous tab
//alert ($(e.relatedTarget).attr('href'));
});
I am able to add class to e.target but e.relatedTarget is not working. Any help?
<div class="list-group" id="MasterPageTabs">
<a href="#lv" class="list-group-item active" data-toggle="tab">Language Variations</a>
<a href="#hm" class="list-group-item" data-toggle="tab">Hotel Master</a>
<a href="#gm" class="list-group-item" data-toggle="tab">Generics Master</a>
</div>
As you can refer, I am not using the default ul li structure for tabs, instead i am using the bootstrap 3 list groups with js for tabs.
update as the code from your comment show the (a) ul is used to select the elements.
You could try to extend / overwrite the show function of the tab plugin. Something like:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="bootstrap3/bootstrap-3.0.0-wip/dist/js/bootstrap.min.js"></script>
<script>
$.fn.tab.Constructor.prototype.show = function () {
var $this = this.element
selector = $this.attr('href')
selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
var previous = $('.list-group').find('.active:last')[0]
var e = $.Event('show.bs.tab', {
relatedTarget: previous
})
$this.trigger(e)
if (e.isDefaultPrevented()) return
var $target = $(selector)
this.activate($target, $target.parent(), function () {
$this.trigger({
type: 'shown.bs.tab'
, relatedTarget: previous
})
})
}
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
$(e.target).addClass('active'); // activated tab
console.log(e.relatedTarget);
$(e.relatedTarget).removeClass('active'); // previous tab
});
</script>
Event.relatedTarget will always be undefined
cause it is only set for mouse events. It has nothing to do with your structure or even Twitter Bootstrap.
See: https://developer.mozilla.org/en-US/docs/Web/API/event.relatedTarget