JQuery UI dynamic tab - Auto select

Okky picture Okky · Jan 29, 2013 · Viewed 9.5k times · Source

I was trying to add a dynamic tab on a button click. I successfully completed it. Now I want my dynamic tab to be selected automatically when its created.

This is My code:

$(document).ready(function(){

    var tabs = $("#container-1").tabs();
    $('#add_tab').click( function(){
            var ul = tabs.find( "ul" );
            $( "<li><a href='#newtab'>New Tab</a></li>" ).appendTo( ul );
            $( "<div id='newtab'>Name :<input type='text'></input></div>" ).appendTo( tabs );
            tabs.tabs( "refresh" );
            tabs.tabs("select", 1);
        });
});

I use CDN links in the order:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>

The problem is that my dynamic tab is not getting selected automatically

The error I get is:

Error: no such method 'select' for tabs widget instance
http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js
Line 2

How do i fix this?

NOTE: my code is working in jsfiddle:http://jsfiddle.net/StDbH/

But not on my system: Fedora 16 + Firefox 18.0.1

Answer

Vic picture Vic · Jan 29, 2013

add this line after refresh:

tabs.tabs( "option", "active", -1 );

http://jsfiddle.net/StDbH/1/