Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Twitter Bootstrap tab, when button is pressed?

I need to change the tab when button is pressed, and the tab should be identified by id. The following code doesn't work for me - just the page is reloaded:

<div class="form-actions">
  <button class="btn btn-primary" id="btn-next">Next</button>
  <button type="reset" class="btn">Clear</button>
</div>
...
<div class="tab-pane active" id="2">
...

$("#btn-next").click(function(event) {
  $('#2').tab('show');
});
like image 855
LA_ Avatar asked Feb 12 '12 19:02

LA_


1 Answers

You can use something like this:

function nextTab(elem) {
  $(elem + ' li.active')
    .next()
    .find('a[data-toggle="tab"]')
    .click();
}
function prevTab(elem) {
  $(elem + ' li.active')
    .prev()
    .find('a[data-toggle="tab"]')
    .click();
}

and use nextTab('#tab'); or prevTab('#tab');

Live example with continue actions: http://jsbin.com/atinel/9/edit#javascript,html

like image 147
balexandre Avatar answered Sep 27 '22 17:09

balexandre