jQuery UI Tabs – How to Get Currently Selected Tab Index

jQuery UI Tabs – How to Get Currently Selected Tab Index

I know this specific question has been asked before, but I am not getting any results using the bind() event on the jQuery UI Tabs plugin.
I just need the index of the newly selected tab to perform an action when the tab is clicked. bind() allows me to hook into the select event, but my usual method of getting the currently selected tab does not work. It returns the previously selected tab index, not the new one:
var selectedTab = $(“#TabList”).tabs().data(“selected.tabs”);

Here is the code I am attempting to use to get the currently selected tab:
$(“#TabList”).bind(“tabsselect”, function(event, ui) {


When I use this code, the ui object comes back undefined. From the documentation, this should be the object I’m using to hook into the newly selected index using ui.tab. I have tried this on the initial tabs() call and also on its own. Am I doing something wrong here?


Solution 1:

For JQuery UI versions before 1.9: ui.index from the event is what you want.

For JQuery UI 1.9 or later: see the answer by Giorgio Luparia, below.

Solution 2:

If you need to get the tab index from outside the context of a tabs event, use this:

function getSelectedTabIndex() { 
    return $("#TabList").tabs('option', 'selected');

From version 1.9 ‘selected’ is changed to ‘active’

$("#TabList").tabs('option', 'active')

Solution 3:

UPDATE [Sun 08/26/2012] This answer has become so popular that I decided to make it into a full-fledged blog/tutorial

Please visit My Blog Here to see the latest in easy access information to working with tabs in jQueryUI

Also included (in the blog too) is a jsFiddle

¡¡¡ Update! Please note: In newer versions of jQueryUI (1.9+), ui-tabs-selected has been replaced with ui-tabs-active. !!!

I know this thread is old, but something I didn’t see mentioned was how to get the “selected tab” (Currently dropped down panel) from somewhere other than the “tab events”.
I do have a simply way …

var curTab = $('.ui-tabs-panel:not(.ui-tabs-hide)');

And to easily get the index, of course there is the way listed on the site …

var $tabs = $('#example').tabs();
var selected = $tabs.tabs('option', 'selected'); // => 0

However, you could use my first method to get the index and anything you want about that panel pretty easy …

var curTab = $('.ui-tabs-panel:not(.ui-tabs-hide)'),
    curTabIndex = curTab.index(),
    curTabID = curTab.prop("id"),
    curTabCls = curTab.attr("class");
        //  etc ....

PS. If you use an iframe variable then .find(‘.ui-tabs-panel:not(.ui-tabs-hide)’), you will find it easy to do this for selected tabs in frames as well.
Remember, jQuery already did all the hard work, no need to reinvent the wheel!

Just to expand (updated)

Question was brought up to me, “What if there are more than one tabs areas on the view?”
Again, just think simple, use my same setup but use an ID to identify which tabs you want to get hold of.

For example, if you have:


And you want the current panel of the second tab set:

var curTabPanel = $('#example-2 .ui-tabs-panel:not(.ui-tabs-hide)');

And if you want the ACTUAL tab and not the panel (really easy, which is why I ddn’t mention it before but I suppose I will now, just to be thorough)

// for page with only one set of tabs
var curTab = $('.ui-tabs-selected'); // '.ui-tabs-active' in jQuery 1.9+

// for page with multiple sets of tabs
var curTab2 = $('#example-2 .ui-tabs-selected'); // '.ui-tabs-active' in jQuery 1.9+

Again, remember, jQuery did all the hard work, don’t think so hard.

Solution 4:

If you’re using JQuery UI version 1.9.0 or above, you can access ui.newTab.index() inside your function and get what you need.

For earlier versions use ui.index.

Solution 5:

When are you trying to access the ui object? ui will be undefined if you try to access it outside of the bind event.
Also, if this line

var selectedTab = $("#TabList").tabs().data("selected.tabs");

is ran in the event like this:

$("#TabList").bind("tabsselect", function(event, ui) {
  var selectedTab = $("#TabList").tabs().data("selected.tabs");

selectedTab will equal the current tab at that point in time (the “previous” one.) This is because the “tabsselect” event is called before the clicked tab becomes the current tab. If you still want to do it this way, using “tabsshow” instead will result in selectedTab equaling the clicked tab.

However, that seems over-complex if all you want is the index. ui.index from within the event or $(“#TabList”).tabs().data(“selected.tabs”) outside of the event should be all that you need.

Solution 6:

var $tabs = $('#tabs-menu').tabs();
// jquery ui 1.8
var selected = $tabs.tabs('option', 'selected');
// jquery ui 1.9+
var active = $tabs.tabs('option', 'active');