tgrass
tgrass

Reputation: 145

Calling twitter bootstrap tab function from within another function

With bootstrap-tab.js I want to activate a tab, calling it from within another function. Html:

<a onClick='activate_tab()'>Activate Tab</a>
<ul class='nav nav-tabs hydro' id='nav_tabs'>
  <li class='active'><a href='#1' data-toggle='tab'>First</a></li>
  <li class=''><a href='#2' data-toggle='tab'>Second</a></li>
  <li class=''><a href='#3' data-toggle='tab'>Third</a></li>
</ul>
<div class='tabbable'>
  <div class='tab-content'>
    <div class='tab-pane active' id='1'>content first</div>
    <div class='tab-pane ' id='2'>content second</div>
    <div class='tab-pane ' id='3'>content third</div>
  </div>
</div>

I am able to activate the tab on page load with the following:

<script>jQuery(function($){$('#nav_tabs li:eq(2) a').tab('show');})</script>

And the tabs activate properly by clicking on the respective anchor elements. The following does not work:

function activate_tab(){
    //process other data...then goto tab
    alert(1);
    $('#nav_tabs li:eq(2) a').tab('show');
    alert(2);
}

I am new to javascript/jQuery.

[EDIT] I stripped the original page down to just what's shown above and it works. So, likely a conflict with other functions. Sorry to waste your time.

Upvotes: 2

Views: 2052

Answers (1)

thecodeparadox
thecodeparadox

Reputation: 87073

try this if you dont

    <script>
    jQuery(function($){

      function activate_tab(){
        //process other data...then goto tab
        alert(1);
        $('#nav_tabs li:eq(2) a').tab('show');
        alert(2);
      }

    });
   </script>

Upvotes: 1

Related Questions