1

HTML

<div class="tabbable tabs-left">
    <ul class="nav nav-tabs">
        <li><a href="#menu23" data-toggle="tab">Beethoven</a></li>
        <li><a href="#menu24" data-toggle="tab">Bach</a></li>
    </ul>

    <div class="tab-content">
        <div class="tab-pane" id="menu23">
            item1
        </div>
        <div class="tab-pane" id="menu24">
            item2
        </div>
    </div>

I want to get the tab index number not tab name.

Such as:

  • if Beethoven is clicked, I need 0;
  • if Bach is clicked, I need 1;

I made this code from this document

$('#myTab a').click(function (e) {
      e.preventDefault();
     // How do I get tab number....??
});
KyleMit
  • 30,350
  • 66
  • 462
  • 664
whitebear
  • 11,200
  • 24
  • 114
  • 237

3 Answers3

5

Try this:

$('.nav-tabs a').click(function (e) {
     e.preventDefault();
     alert($($(this).attr('href')).index());
});

Or with bootstrap's events

$('a[data-toggle="tab"]').on('shown', function (e) {
  $(e.target).index();
})
KyleMit
  • 30,350
  • 66
  • 462
  • 664
omma2289
  • 54,161
  • 8
  • 64
  • 68
  • you need to use: $(document).on('shown.bs.tab', ....) - https://stackoverflow.com/questions/23211543/tab-shown-event-not-firing-twitter-bootstrap – Edd Jul 31 '18 at 16:10
0

If you want to do it without a click event you can use this:

var tabIdx = $('.dashboardTab.active').data("tabidx");
console.log(tabIdx);
Rocco
  • 187
  • 1
  • 4
-1

We have direct API for this.

currentIndex    

Returns the zero based index number for the current tab.
for more- https://github.com/VinceG/twitter-bootstrap-wizard

S.Yadav
  • 4,273
  • 3
  • 37
  • 44