JQuery UI dynamic tab - Auto select
Asked Answered
P

2

5

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

Punkie answered 29/1, 2013 at 5:39 Comment(0)
C
9

add this line after refresh:

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

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

Cleres answered 29/1, 2013 at 5:59 Comment(1)
take a look at jquery UI's tab api: api.jqueryui.com/tabs/#option-active There's an "active" option that allows you to select which tab to display. If the index is negative, then it starts from the last tab, which is why I have it as -1Cleres
M
1

try show() options of tab

$( ".selector" ).tabs({ show: { effect: "blind", duration: 800 } });    
Misusage answered 29/1, 2013 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.