PhDJ
PhDJ

Reputation: 313

jQueryUI tabs : clear div of other tabs when tab is selected

I have a page with several tabs that show similar content, to make sure I access the correct fields in the correct tab, I want to clear the contents of the divs of the other tabs as soon as the user clicks a tab.

This is the code I use :

<!doctype html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>WebShop</title>
     <link rel="stylesheet" type="text/css" href="/includes/igepa.css" />
     <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
     <link rel="stylesheet" href="/resources/demos/style.css" />


    <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>    

    <script>
    $(function() {
        $( "#tabs" ).tabs({
            beforeLoad: function( event, ui ) {
                ui.jqXHR.error(function() {
                    ui.panel.html(
                        "Couldn't load this tab. We'll try to fix this as soon as possible. " +
                        "If this wouldn't be a demo." );
                });
            },
            load: function(event, ui){
               $('.ui-tabs-hide').empty();  
            }                          
        });
    });
    </script>
</head>
<body>

<div id="tabs">
    <ul>
        <li><a href="/itemStructPaper/Enveloppen">Enveloppen</a></li>
        <li><a href="/itemStructPaper/GrafischKarton">Grafisch karton</a></li>
    </ul>
</div>


</body>
</html>

I have added an example here : http://www.igepa.be/phdj/tabs/index.htm

page1.htm and page2.htm both have a form with a the same field on them but a different value, but when you select the tab for the second page you get the value of the field of the first page.

Upvotes: 1

Views: 5523

Answers (1)

You can do it using the ui parameter and referring to its siblings, like this:

   beforeLoad: function(event, ui){
      $(ui.panel).siblings('.ui-tabs-panel').empty();
    }

Update: I've just updated my code above to just change load for beforeLoad event so the siblings tab contents are removed before the page of the current tab is executed.

Upvotes: 6

Related Questions