tags:

views:

14039

answers:

4

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?

+6  A: 

I have created a demo for you online so you can compare it with what you are doing, hopefully it will highlight what you need to change. If not can you try to re-create your issue at the same site and I will look into it.

edit: in short: ui.index from the event is what you want.

redsquare
Very nice answer! I included a summary of what you did in the website just to make it easier to get the answer.
torial
Cheers, the Q mentioned the ui object was null, therefore ui.index will fail at present. I think the answer maybe not so simple as including that.
redsquare
This was a perfect answer, and thanks for the awesome example! I was trying to do everything in 1 shot, and it wasn't working. After I split it out, everything worked as advertised.
Mark Struzinski
That's great... your link doesn't work anymore. Why don't you just post the answer so people to come can see the solution as well.
Ryan
The answer is there - use ui.index property to get the current index in the tabselect event.....
redsquare
+4  A: 

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.

Ben Koehler
@Ben : your solution gives the previous selected tab as that was the index when the tabsselect event was triggered.
Michael Mao
@Michael: Did you read my answer or just grab the code? In my answer I state that the code will not work as is and provide a few alternatives ('tabshow' event; ui.index; $('TabList').tabs().data('selected.tabs'))
Ben Koehler
@Ben : Sorry about my late response. Yeah, the ui.index works just fine. I just wanted to point out the fact that selected.tabs gives the "previous" selected tab, not the cureent one. I mean no offense to your answer.
Michael Mao
A: 

Wish the link still worked as I am having the same problem and can't see your solution :(

+5  A: 
function getSelectedTabIndex() { 
    return $tabs.tabs('option', 'selected');
}
Contra
That appeared to be what I needed, anyway.
El Yobo