11

I have a JSP page which uses bootstraps data-toggle="tab" functionality. Upon page load I make one tab active.

<ul class="nav st-nav-tabs">
    <li class="active"><a href="#tab1" data-toggle="tab">First Tab</a></li>
    <li><a href="#tab2" data-toggle="tab">Second Tab</a></li>
</ul>

<div class="tab-content">
    <div class="tab-pane active" id="tab1"></div>
    <div class="tab-pane active" id="tab2"></div>
</div>

I have a Highcharts chart on the page, whenever a user clicks on one of the columns I call a JS function which makes tab2 the active tab on the page. Inside this function I've tried a few different commands but none seem to work. I'm quite new to jQuery so I'm hoping someone might be able to point out where I'm going wrong with my syntax.

function handleClick(){
    //I've tried the following, none seem to work
    $('#tab2').toggleClass('active');
    $('#tab2').show();
    $('#tab2').tab('show');
}
1
  • 2
    Call the .click() method for the actual link: $('#mynav a[href="#tab2"]').click()
    – scragar
    Jul 23, 2014 at 12:44

1 Answer 1

21

First you need to give your tabs an id, i will call it myTabs:

<ul class="nav st-nav-tabs" id="myTabs">

Then you can call and show your tabs by name to show them:

$('#myTabs a[href="#name"]').tab('show');

You can read about tabs in bootstrap's documentation.

2
  • @180cl , you're welcome. Don't forget to mark the checkmark to the left of the answer to show it helped for future users.
    – Jordan.J.D
    Jul 23, 2014 at 12:56
  • From my experience, this can't be called directly from the Jquery. If you want this to work it has to be from a click or other action. this won't work: <script> $('#myTabs a[href="#name"]').tab('show'); </script> it needs a click, done or other actions to be activated. Jul 31, 2017 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.