16

I'm having trouble trying to get this functionality to work. I would like to load a tab automatically when the page loads. I have used the following code.

$tabs.tabs('select', 0); 

However, what's happening is that it's only selecting it and not loading it (that tab loads via ajax). Interestingly when I select another tab and then go back to the 1st tab again then it loads fine.

1
  • just to add, when I do the following then the 1st tab loads fine. $tabs.tabs('select',1); $tabs.tabs('select',0); But I really want to just use $tabs.tabs('select',0); Any ideas? Commented Jun 9, 2009 at 15:41

7 Answers 7

20

I just answer to this question to avoid people to misguide with deprecate functions.

Most of the answers were correct at the time of the question, but most of them were not using in new API.

try following code If you are using new API.

$(function() { 
    $( "#tabs" ).tabs(); 
    $( "#tabs" ).tabs( "option", "active", 2 ); 
});

For more details check following link.

http://jqueryui.com/upgrade-guide/1.9/#deprecated-select-method

Sign up to request clarification or add additional context in comments.

5 Comments

I use your solution, however, it just breaks my tabs. I have this as a function $(document).ready(function() { $( "#tabs" ).tabs("option","active",2); }); but it shows the content of all the tabs at once
What is your jquery version ?
jquery-1.9.1, jquery-ui-1.10.3
First you need to initialize the tabs, then my code. full code like this $(function() { $( "#tabs" ).tabs(); $( "#tabs" ).tabs( "option", "active", 2 ); });
I'd give you multiple upvotes + an answer if I could. I might create a new question, with the answer provided, and link here though :-)
17

Have you tried specifying the selected tab at the time of initialization:

var $tabs = $('.selector').tabs({ selected: 0 });

After initialization, you can do this to programmatically select a tab:

$tabs.tabs('option', 'selected', 0);

EDIT: This works perfectly for me:

var $tabs = jQuery("#tabDiv > ul").tabs( { selected: null } );
$tabs.tabs("select", 0);

7 Comments

I have tried all the suggestions on this page and none are working. Could it be because I have nested tab. Basically, parent tab calls a page which has more tabs and I would like to select the 1st tab of this child tabs page. It's not working. Only after selecting another tab and then selecting the 1st tab programatically seems to work.
Just so that everyone is clear. It does select the 1st tab but doesn't load the page that it's supposed to load via ajax
Ok. I have a thought in mind. Let me know if that makes sense. Could it be that jquery tabs default to selecting the 1st tab. So that when you programatically try to select it again, it doesn't load it?
@Saumil, I have this working, it must be one of two possibilities. I have edited my answer.
Thanks Karim. Setting the selected option to NULL and then selecting the 1st tab did the trick. So far it's been working fine. I'll let you know if I run into any other issues. Thank you!
|
2

I had the same problem before. The solution I had was calling tabs() two times; once without parameters; once with the select option:

Eg.

$("#tabs").tabs();
$("#tabs").tabs('select', 2);

Comments

1

If nothing works, here's a little trick:

You can use adding and removing CSS classes from jquery like below:

// Firstly, remove the "active" class from div panel and <li> tag
$('#firstTab, #firstTabDiv').removeClass('active');
Also, remove the "in" class from div tag in which your panel resides
$('#firstTabDiv').removeClass('in');

Then add same class which you removed in the div and panel in which you want 
to get automatically select
$("#thirdTab, #thirdTabDiv").addClass('active');
$("#thirdTabDiv").addClass('in');

<li id='firstTab' class="active">
    <a href="#firstTabDiv" data-toggle="tab">
        First Tab
    </a>
</li>
<li id='secondTab' class="active">
    <a href="#secondTabDiv" data-toggle="tab">
        Second Tab
    </a>
</li>
<li id='thirdTab' class="active">
    <a href="#thirdTabDiv" data-toggle="tab">
        Third Tab
    </a>
</li>

This way you will be able to automatically show a tab onload.

Comments

0

Don't forget to put it inside of a $(document).ready(function(){;}); construct.

I'm sure that's not the cause of your problem, but it can't hurt to make sure your jQuery isn't firing until everything is set up to handle it.

Comments

0

You should be able to call:

$tabs.tabs('load', 0);

after you select it to load its data.

2 Comments

I thought selecting a tab also loads it.
I tried it anyway and nope still doesn't work. Only after selecting any other tab and then going back to select the 1st tab seems to work. Weird!!!
0

Assuming you were using the demo from the Jquery tabs page (http://jqueryui.com/tabs/#default). You can easily select the tab by clicking on the anchor. Just find the anchor tag based on its tab name like so...

$('a[href=#tabs-1]').click(); //selects the first tab

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.