16
votes

I want to change the active bootstrap tab on the click of another button.

I have tried add id's to the tabs li and write custom jquery code.

$('#emailNotify').click(function () {
    $('#notify').addClass('active').attr('aria-expanded','true');
    $('#myacc').removeClass('active').attr('aria-expanded','false');
});

This code works fine on first click but it doesn't change back when I tried to click My Account tab again.

Image of Tabs and Button

Markup:

<ul class="content-list museo_sans500">
<li><a href="javascript:void(0)">Change Password</a></li>
<li><a id="emailNotify" data-toggle="tab">Change Email Notifications</a></li>
<li><a href="javascript:void(0)">Change Profile Picture</a></li>
</ul>
2
Could you please add the markup ? - DinoMyte
I have added the markup. @DinoMyte - Farjad Hasan
' $( "#tabs" ).tabs({ active: 0 });' This the way I do it in jquery. this would be first tab - Fintan Creaven
@NuttyProgrammer are you using data-toggle="tab" for your tabs - bigOther
@bigOTHER Yes, I am using data-toggle='tabs' - Farjad Hasan

2 Answers

29
votes

You can change the active tab on the click event button with that:

$('#changetabbutton').click(function(e){
    e.preventDefault();
    $('#mytabs a[href="#second"]').tab('show');
})

Here's a JSFiddle with an example:

http://jsfiddle.net/4ns0mdcf/

2
votes

You should use twitter-boostrap way to change between tabs:

$('#myTab a:first').tab('show'); // Select first tab
$('#myTab a:last').tab('show'); // Select last tab

or

$('#notify').tab('show'); // show notification tab