Okky
Okky

Reputation: 10466

JQuery UI tab auto close

I'm doing a work on JQuery UI tabs. I want to close a dynamically generated tab after form submission.

This is my code:

function submit_form(){
    alert(JSON.stringify($('form').serializeObject()));
    $('#result').text(JSON.stringify($('form').serializeObject()));
    var tabs = $("#container-1").tabs();
    tabs.tabs('remove', 1);
}

$(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'><form action='' method='post'>Name :<input type='text' name='name'></input></br>Email :<input type='text' name='email'></input></br>Phone Number :<input type='text' name='phone'></input></br><input type='button' value='Submit' id='form_button' onclick='submit_form()'></input></form></div>" ).appendTo( tabs );
            tabs.tabs( "refresh" );
            tabs.tabs( "option", "active", -1 );
    });
});

My HTML page:

<body>

<div id="container-1">
    <ul>
        <li><a href="#fragment-1">List</a></li>
    </ul>   
    <div id="fragment-1">
        <table id="contact">
        <tr>
        <th> Name </th>
        <th>E-mail </th>
        <th> Phone </th>
        </tr>
        </table>
    </div>
</div>

Add Tab

The tab I want to close is dynamically created by the function on-click of add_tab. I want to close it at the end of function submit_form().

It should have been removed with the code

tabs.tabs('remove', 1);

But I'm getting an error

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

How to do I fix it?

Upvotes: 2

Views: 2683

Answers (1)

Jetson John
Jetson John

Reputation: 3829

The problem is that in the new jquery Ui the remove function has been deprecated use refresh method.

Old API:

$( "#tabs" ).tabs( "remove", 2 );

New API:

// Remove the tab
var tab = $( "#tabs" ).find( ".ui-tabs-nav li:eq(2)" ).remove();
// Refresh the tabs widget
$( "tabs" ).tabs( "refresh" );

Try fixing your issue with this in mind.

Upvotes: 7

Related Questions