52

If I have three tabs:

<div id="tabs">
    <ul>
        <li><a href="#sample-tab-1"><span>One</span></a></li>
        <li><a href="#sample-tab-2"><span>Two</span></a></li>
        <li><a href="#sample-tab-3"><span>Three</span></a></li>
    </ul>
</div>

I would like to swap to #sample-tab-2 by it's name. I know I can switch to a tab if I know it's number, but in the case I've run into I won't know that.

Notes: JQuery 1.3.1 / JQuery-UI 1.6rc6

Rob
  • 2,332
  • 7
  • 31
  • 35

16 Answers16

87

I could not get the previous answer to work. I did the following to get the index of the tab by name:

var index = $('#tabs a[href="#simple-tab-2"]').parent().index();
$('#tabs').tabs('select', index);
  • 13
    This answer is no longer the "shortest" way to do it, as jQueryUI simply evolved since 2010. if you are one of those who take the selected / highest voted answer for granted and don't scroll down to others, here is a nice syntactic sugar that was added since: `$("#tabs").tabs("select", "#sample-tab-1");` – Eran Medan Oct 14 '12 at 16:11
  • 12
    JqueryUI has evolved yet again, here is the new solution: `$("#tabs").tabs("option", "active", $(tabName + "Selector").index());`, see this jsfiddle for more info: http://jsfiddle.net/vpJC3/ – JBCP Feb 08 '13 at 03:47
  • 2
    You have to deduct 1 from the index. Because the array index and the element index differs by 1. Write like this: $("#tabs").tabs().tabs("option", "active", $("#Tab-1").index() - 1); – Rahatur Jan 01 '14 at 18:36
  • Bu this code i'm getting this error.... cannot call methods on tabs prior to initialization; attempted to call method 'select' .. Do u know abt that @christian – Vicky Mar 21 '14 at 09:03
  • NB: Solutions that use the index of the target display element rely on the display elements being in the same order as the list elements! – Mark May 23 '14 at 04:41
34

It seems that using the id works as well as the index, e.g. simply doing this will work out of the box...

$("#tabs").tabs("select", "#sample-tab-1");

This is well documented in the official docs:

"Select a tab, as if it were clicked. The second argument is the zero-based index of the tab to be selected or the id selector of the panel the tab is associated with (the tab's href fragment identifier, e.g. hash, points to the panel's id)."

I assume this was added after this question was asked and probably after most of the answers

Eran Medan
  • 44,555
  • 61
  • 184
  • 276
  • 2
    Your example should just be `$("#tabs").tabs("select", "#sample-tab-1");`, as per the [newest docs](http://docs.jquery.com/UI/Tabs#method-select). – iano Apr 24 '12 at 21:21
  • @iano You are correct... docs seems to have changed... thanks – Eran Medan Apr 25 '12 at 00:40
  • @JBCP looks good, but I was afraid this is how it will be. why did they regress to a less convenient API without any explanation, I'm sure they have some sort of explanation, I can't think of any. – Eran Medan Feb 08 '13 at 05:52
26
$('#tabs').tabs('select', index); 

Methods `'select' isn't support in jquery ui 1.10.0.http://api.jqueryui.com/tabs/

I use this code , and work correctly:

  $('#tabs').tabs({ active: index });
hamid reza mansouri
  • 11,035
  • 2
  • 22
  • 32
  • Good that hamid mentioned the jquery ui version. Too bad that they recently changed the API and removed basic functionality like adding tabs (deprecated in 1.9, taken out in 1.10). I don't think I will be able to see past this into their philosophizing. Like selling a car with a powertrain but no seats. – kovacsbv Feb 06 '14 at 09:55
12

You can get the index of the tab by name with the following script:

var index = $('#tabs ul').index($('#simple-tab-2'));
$('#tabs ul').tabs('select', index);
bdukes
  • 152,002
  • 23
  • 148
  • 175
  • This does not work with me. The answer of Christian George did work. What version of jquery are you using? I am using 1.4.3. – Nap Jun 10 '11 at 09:54
  • @Nassign yeah, looks like my answer doesn't work (it's trying to select the li as though it has the ID `simple-tab-2`, instead of having that as an `href`). – bdukes Jun 10 '11 at 13:27
  • Is there a way to change tabs using this code while anchoring the page? – Michael Capobianco Jun 19 '12 at 13:04
  • The issue with getting index() of the panel ID is that index() returns the index of the element in respect to the parent HTML element so you might be thinking the first panel returns 0, but in fact it will return 1 because index() will account for the UL above – experimenter Feb 05 '14 at 11:16
6

Only this code real works!

$('#tabs').tabs();
$('#tabs').tabs('select', '#sample-tab-2');
xmoonlight
  • 199
  • 4
  • 12
5

Use this function:

function uiTabs(i){
    $("#tabs").tabs("option", "selected", i);
}

And use following code to switch between tabs:

<a onclick="uiTabs(0)">Tab 1</a>
<a onclick="uiTabs(1)">Tab 2</a>
<a onclick="uiTabs(2)">Tab 3</a>
Hamidreza
  • 1,825
  • 4
  • 29
  • 40
4

try this: "select" / "active" tab

<article id="gtabs">
    <ul>
        <li><a href="#syscfg" id="tab-sys-cfg" class="tabtext">tab One</a></li>
        <li><a href="#ebsconf" id="tab-ebs-trans" class="tabtext">tab Two</a></li>
        <li><a href="#genconfig" id="tab-general-filter-config" class="tabtext">tab Three</a></li>
    </ul>

var index = $('#gtabs a[href="#general-filter-config"]').parent().index();

// `'select' does not support in jquery ui version 1.10.0

$('#gtabs').tabs('select', index);  

alternate solution: use "active":

$('#gtabs').tabs({ active: index });
Muru Bakthavachalam
  • 1,340
  • 12
  • 8
3

The only practical way to get the zero-based index of your tabs is to step through each of the elements that make the tabset (the LI>A s) and match on their inner text. It can probably be done in a cleaner way, but here's how I did it.

$('#tabs ul li a').each(function(i) {
    if (this.text == 'Two') {$('#reqTab').val(i)}
});

$("#tabs").tabs({
    selected: $('#reqTab').val()
});

You can see that I used a hidden <input id="reqTab"> field in the page to make sure the variable moved from one function to the other.

NOTE: There is a little bit of a gotcha -- selecting tabs after the tabset is activated doesn't seem to work as advertised in jQuery UI 1.8, which is why I used the identified index from my first pass in order to initialize the tabset with the desired tab selected.

Wes
  • 31
  • 2
3

The following piece worked for me

$($("#tabs")[0]).tabs({selected: 1});

Hope, this helps!

Sandeep
  • 31
  • 1
1

If you are changing the hrefs, you can assign an id to the links <a href="#sample-tab-1" id="tab1"><span>One</span></a> so you can find the tab index by it's id.

kiev
  • 2,040
  • 9
  • 32
  • 54
1
$('#tabs a[href="#sample-tab-1"]').click();

or, you can assign an id to the links

<a href="#sample-tab-1" id="tab1">span>One</span></a>

so you can find it's id.

$('#tab1').click();
Dimasbka
  • 11
  • 2
1

Set specific tab index as active:

$(this).tabs({ active: # }); /* Where # is the tab index. The index count starts at 0 */

Set last tab as active

$(this).tabs({ active: -1 });

Set specific tab by ID:

$(this).tabs({ active: $('a[href="#tab-101"]').parent().index() });
Carl
  • 805
  • 12
  • 24
1

@bduke's answer actually works with a slight tweak.

var index = $("#tabs>div").index($("#simple-tab-2"));
$("#tabs").tabs("select", index);

Above assumes something similar to:

<div id="tabs">
  <ul>
    <li><a href="#simple-tab-0">Tab 0</a></li>
    <li><a href="#simple-tab-1">Tab 1</a></li>
    <li><a href="#simple-tab-2">Tab 2</a></li>
    <li><a href="#simple-tab-3">Tab 3</a></li>
  </ul>
  <div id="simple-tab-0"></div>
  <div id="simple-tab-1"></div>
  <div id="simple-tab-2"></div>
  <div id="simple-tab-3"></div>
</div>

jQueryUI now supports calling "select" using the tab's ID/HREF selector, but when constructing the tabs, the "selected" Option still only supports the numeric index.

My vote goes to bdukes for getting me on the right track. Thanks!

cautionbug
  • 435
  • 5
  • 18
1

Here is a sample code to get the selected tab by name. I hope this aids you to find ypur solution:

<html>
<head>
<script type="text/javascript"><!-- Don't forget jquery and jquery ui .js files--></script>
<script type="text/javascript">
   $(document).ready(function(){
     $('#tabs').show();

     // shows the index and tab title selected
     $('#button-id').button().click(function(){
         var selTab = $('#tabs .ui-tabs-selected');
         alert('tab-selected: '+selTab.index()+'-'+ selTab.text());
     });
  });
</script>
</head>
<body>
   <div id="tabs">
      <ul id="tablist">
            <li><a href='forms/form1.html' title="form_1"><span>Form 1</span></a></li>
            <li><a href='forms/form2' title="form_2"><span>Form 2</span></a></li>
      </ul>
   </div>
    <button id="button-id">ClickMe</button>
</body>
</html>
Vikram
  • 4,162
  • 8
  • 43
  • 65
0

I had trouble getting any of the answers to work as they were based on the older versions of JQuery UI. We're using 1.11.4 (CDN Reference).

Here is my Fiddle with working code: http://jsfiddle.net/6b0p02um/ I ended up splicing together bits from four or five different threads to get mine to work:

    $("#tabs").tabs(); 

    //selects the tab index of the <li> relative to the div it is contained within
    $(".btn_tab3").click(function () {
        $( "#tabs" ).tabs( "option", "active", 2 );
    });         

    //selects the tab by id of the <li>
    $(".btn_tab3_id").click(function () {
      function selectTab(tabName) {
          $("#tabs").tabs("option", "active", $(tabName + "").index());
      }

      selectTab("#li_ui_id_3");
    });
0

This works for Bootstrap v5 Tabs:

To select tab id: tab-2

$('.nav-tabs a[href="#tab-2"]').tab('show');
Sharhabeel Hamdan
  • 1,273
  • 13
  • 15