2

I have the following JavaScript function to select a tab by its index:

function changeTab(tabIndex) {
    $("#panel").tabs("select", tabIndex);
}

But I have to check if that tab exists and if it's visible, or else it would try to select a tab that shouldn't be available.

For the existence it's fine, I just have to use $("#panel").tabs("length");, but to check if it is visible I would need the element itself.

Community
  • 1
  • 1
Carcamano
  • 1,064
  • 1
  • 10
  • 22

3 Answers3

2

It seems there isn't a built-in method for that, but I was able to accomplish it by navigating to the tab:

function changeTab(tabIndex) {
    var panel = $("#panel");
    var queryIndex = tabIndex + 1; // 1 based
    var desiredTab = panel.find("> ul li:nth-child( " + queryIndex + ")");

    if (desiredTab && desiredTab.is(":visible")) {
        panel.tabs("select", tabIndex);
    }
}
Carcamano
  • 1,064
  • 1
  • 10
  • 22
0

First get the Active index

var activeIndex = $("#panel").tabs('option', 'active');

Then using the css class get the tab content panel

// this will return the html element
var element=   $("#panel").find( ".ui-tabs-panel" )[activeIndex]; 

now wrapped it in jQuery object to further use it

 var tabContent$ = $(element);

here i want to add two info the class .ui-tabs-nav is for Navigation associated with and .ui-tabs-panel is associated with tab content panel. in this link demo in jquery ui website you will see this class is used - http://jqueryui.com/tabs/#manipulation

dekdev
  • 4,835
  • 2
  • 25
  • 32
0

I haven't tested but you should be able to get the index of the selected tab by

var activeIndex = $("#panel").tabs( "option" , 'selected')

And then you should be able to just very that activeIndex equals tabIndex

Mikael Eliasson
  • 4,887
  • 20
  • 27
  • Doesn't help because that will happen after the tab has been selected, which is what I want to avoid. I found a way by navigating to the tab, but I won't be able to answer my own question in the next 5 hours because of my low reputation. – Carcamano Jun 30 '11 at 15:54