views:

304

answers:

1

I am having a problem with Jquery Tabs. On my MVC page, I have a form that returns a partial page (.ascx). This page has Jquery tabs on it, howerver all I get is the tab content without the tab. It looks like the partial page javascript code for the tab does not run,

<script type="text/javascript">
    $(function() { $("#tabs").tabs(); });
</script>

I tried to put this in the main page, but I get an error because "#tabs" does not exist on the page when it is first loaded.

Any ideas on getting this to work?

+1  A: 

Are you loading the partial via AJAX? If so, put the code that generates the tabs in the AJAX callback instead of on the page.

$('#tabContent').load( '/controller/action', null, function() {
   $('#tabs').tabs();
});

With the MS Ajax helpers:

<% using (Ajax.BeginForm("Action", new AjaxOptions { ..., OnSuccess = "doTabs", ...})) %>

<script type="text/javascript">
    function doTabs() {
       $('#tabs').tabs();
    }
</script>
tvanfosson
I am using an Ajax.BeginForm
Tony Borf
Then, you'll want to add the tabs() function in the success callback specified in the AjaxOptions.
tvanfosson
Will this still work if the Ajax.BeginForm can return a few different pages and only one of them has tabs?
Tony Borf
If you don't have a #tabs element on the page, the selector won't match anything and the tabs won't be applied.
tvanfosson
Worked great, thanks.
Tony Borf