shasi kanth
shasi kanth

Reputation: 7094

Clear the jquery dialog content on close

When i click on a link in my page, i am opening a jquery dialog box (if it is not already open) and adding a jquery ui tab inside it. Followed example from here

But when i close my jquery dialog and reopen it, the tabs which were opened previously are showing up. I would like to have no tabs inside the jquery dialog, when i close and reopen the dialog, so that i can add a new tab inside the dialog.

This is the code that is present in my jquery document ready:

<script type="text/javascript">
var tab_counter = 1;

$(document).ready(function() {
var $tabs_example_1 = $('#example_1').tabs();

$('#addUiTab').click(function(){
        var label = 'Tab'+tab_counter,
        content = '<span id="tab">This is a sample tab content</span>';

        $('body').append('<div id="tab_'+tab_counter+'">'+content+'</div>');
        $tabs_example_1.tabs('add','#tab_'+tab_counter,label);
        tab_counter++;

        return false;
    });
});
</script>

This is my function that opens a dialog box (if it is not already open) and adds a tab inside it, when i click on a link in my page:

function open_dialog()
{
    if(($("#jdialog_box_content").dialog( "isOpen" ) === true) == false)
    {
        $('#jdialog_box_content').dialog({
        open: function(event, ui) {  },
        close: function(event, ui) {  },
        autoResize: false,
        width: 460,
        height: 230,
        closeOnEscape: false,
        title: 'Dialog1'
        });
    }

    $('#addUiTab').trigger("click");
}

And finally this is the html that is used for the jquery tabs, inside jquery dialog box:

<div id="jdialog_box_content" style="display:none;">    
    <div id="example_1" class="tabs">
        <ul>
            <li><a href="#tabs-1-2">Tab 1</a></li>
            <li><a href="#tabs-2-2">This is tab 2</a></li>
            <li><a href="#tabs-3-2">This is tab number 3</a></li>           
        </ul>
        <div id="tabs-1-2">Tab 1<br>Lorem ipsum dolor sit amet</div>
        <div id="tabs-2-2">This is tab 2<br>Lorem ipsum dolor sit amet</div>
        <div id="tabs-3-2">This is tab 3<br>Lorem ipsum dolor sit amet</div>
    </div>
</div>

I am just curious to know how can i remove the existing tabs in my jquery dialog box, when it is reopened.

Note: I am following the example at this site for jquery ui tabs.

Upvotes: 1

Views: 13560

Answers (2)

shasi kanth
shasi kanth

Reputation: 7094

Thanks, i could also use a for loop code under the close event of the jquery dialog, to close all the open tabs:

close: function(event, ui) 
{ 
    for (var i = $('#example_1').tabs('length') - 1; i >= 0; i--) {
    $('#example_1').tabs('remove', i);                  
    }
}

Upvotes: 0

core1024
core1024

Reputation: 1882

You need close: function(event, ui) { $("#jdialog_box_content").html(""); } in your dialog declaration. if you don't need empty dialog and just no tabs you can do so by that:

$('#jdialog_box_content').dialog({
        open: function(event, ui) {  },
        close: function(event, ui) {
            var $tabs = $('#example_1');
            var l = $tabs.tabs('length');
            while(l)
            {
               $tabs.tabs('remove', l-1);                       
               l = $tabs.tabs('length');
            }

        },
        autoResize: false,
        width: 460,
        height: 230,
        closeOnEscape: false,
        title: 'Dialog1'
        });

Upvotes: 5

Related Questions