Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Dynamic tab in jquery-ui tab

I was doing a project on HTML and jQuery recently. Thing I want to achieve now is to create dynamic tab with particular data on a button click.

My code for JQuery-UI tab is

$(document).ready(function() {
        var $tabs = $("#container-1").tabs();
            var tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
            tabCounter = 2;

        $('#add_tab').click( function(){   
             var label = 'New',
            id = "tabs-" + tabCounter,
            li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
            tabContentHtml = 'hi';
            tabs.find( ".ui-tabs-nav" ).append( li );
            tabs.append( "<div id='" + id + "'><p>" + tabContentHtml + "</p></div>" );
            tabs.tabs( "refresh" );
            tabCounter++;
        });
        $('#new').click( function(){
            $tabs.tabs('select', 2);
        });

    }); 

My HTML file

<div id="container-1">
    <ul>
        <li><a href="#fragment-1">List</a></li>
    </ul>

    <div id="fragment-1">

    </div>


</div>

<button id="add_tab">Add Tab</button>

When i click 'add' button in the console of firebug I'm get error:

ReferenceError: tabs is not defined
http://localhost:3000/
Line 38

I'm not so good with jquery-ui. How do I fix this problem?

like image 317
Okky Avatar asked Jan 28 '13 11:01

Okky


2 Answers

The problem is with your script.So try this

$(document).ready(function() {
        var tabs = $("#container-1").tabs();
            var tabCounter = 1;

        $('#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);
        });


    }); 
like image 188
Nithin Viswanathan Avatar answered Sep 21 '22 18:09

Nithin Viswanathan


the only problem in your code was that you defined in the beginning $tabs and later called for tabs without the dollar sine. I just removed the dollar sine and it works the way you expected (note the var tabs definition on second line) ... I aswel added code to the jsfiddle.

$(document).ready(function() {
    var tabs = $("#container-1").tabs();
        var tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
        tabCounter = 2;

    $('#add_tab').click( function(){   
        var label = 'New',
        id = "tabs-" + tabCounter,
        li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
        tabContentHtml = 'hi';
        tabs.find( ".ui-tabs-nav" ).append( li );
        tabs.append( "<div id='" + id + "'><p>" + tabContentHtml + "</p></div>" );
        tabs.tabs( "refresh" );
        tabCounter++;
    });
    $('#new').click( function(){
        $tabs.tabs('select', 2);
    });

}); 
like image 44
tobif Avatar answered Sep 20 '22 18:09

tobif