Zach
Zach

Reputation: 447

jquery ui tab select method not working

I have two tabs with a submit button on each tab. When the button is clicked, I need to reload the content of that specific tab to get updated data from the server.

if (validStatus()) {
    $.ajax({
        //...
        success: reloadTab
    });
}    

function reloadTab() {                
            var currentTab = $("#tabs").tabs("option", "active");
            alert(currentTab);
            $('#tabs').tabs('select', currentTab);
            alert(currentTab);
        }

When the button is clicked, the tab doesn't refresh. I see the first alert but not the second.

HTML is as follows:

Head:

<link rel="stylesheet" href="@this.Url.Content("//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css")" />
<script>
    $(function () {
        $("#tabs").tabs();
    });
</script>

Body:

<div id="tabs">
<ul>
    <li><a href="#Tab1" title="Tab1">The first tab</a></li>
    <li><a href="#Tab2" title="Tab2">the second tab</a></li>
    <li><a href="#Success" title="Success">Success</a></li>
</ul>

<div id="Success">
    testing
</div>

<div id="Tab1">

    <fieldset >
        <legend>Overview</legend>
        <input type="button" id="submit1" value="submit" />
        <br />
    </fieldset>

    <fieldset style="width: 700px;">
        <legend>Overview</legend>
        <div>
            <table >
            //updated with ajax
            </table>
        </div>
    </fieldset>

    <script>
        //reloadTab is in here
    </script>
</div>

<div id="Tab2">

    <fieldset style="float:left; width:300px;">
        <input id="submit2" type="button" value="submit"/>
    </fieldset>

    <fieldset style="float:left;">
        <legend>Overview</legend>
        <table>
        //updated with ajax
        </table>
    </fieldset>

    <script>.....</script>
</div>

Upvotes: 0

Views: 11844

Answers (2)

Zach
Zach

Reputation: 447

Turns out tabs.('select', ...) is deprecated, using tabs.('option', 'active', index) fixed my issue. Solution found in this comment: https://stackoverflow.com/a/16033969/1463649

Upvotes: 5

Andrei Cristian Prodan
Andrei Cristian Prodan

Reputation: 1122

Do you see anything in the console of your browser? What browser are you using?
Try this to help you with the debugging.

function reloadTab() {    

console.log($('#tabs')); // if this is an empty object, the element doesn't exist when you call this function
console.log($('#tabs').tabs()); // if this doesn't return 'function', you haven't included a library properly, maybe jquery ui, or jquery, or you're using an old version or something
console.log(currentTab); // if this is undefined then something went wrong and no tab is active            
        var currentTab = $("#tabs").tabs("option", "active");
        alert(currentTab);
        $('#tabs').tabs('select', currentTab);
        alert(currentTab);
    }

Upvotes: 0

Related Questions