views:

481

answers:

3

Hi

I'm new to jQuery (a couple of weeks). I'm trying to nest the jQueryUI tab control. It works fine until the nested tabs are in an external file then I get an exception thrown in the jQuery source. This seems to be a timing issue to a certain extent, because if I place alert boxes in the .ready function of the nested tabs it works. Can anyone help? I'm sure this question must have been asked before, but after hours of searching I can't seem to find a solution.

Here's my very simple example...

Outer Tab

    <head id="Head1" runat="server">
    <title></title>
    <link type="text/css" href="css/jquery-ui-1.7.2.custom.css"
      rel="stylesheet" />
    <script type="text/javascript" src="http://jqueryui.com/latest/
    jquery-1.3.2.js"></script>
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/
       ui.core.js"></script>
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/
       ui.tabs.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#OuterTab").tabs();
        });
    </script>
    </head>
<body>
  <div id="OuterTab">
    <ul>
      <li><a href="innerTab1.aspx" title="InnerTab1"><span>InnerTab1</
         span></a></li>
      <li><a href="innerTab2.aspx" title="InnerTab2"><span>InnerTab2</
         span></a></li>
    </ul>
    <div id="InnerTab1">
    </div>
    <div id="InnerTab2">
    </div>
  </div>
</body>
</html>

InnerTab1.aspx

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    <link type="text/css" href="css/jquery-ui-1.7.2.custom.css"
      rel="stylesheet" />
    <script type="text/javascript" src="http://jqueryui.com/latest/
      jquery-1.3.2.js"></script>
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/
      ui.core.js"></script>
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/
      ui.tabs.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#tabs").tabs();
        });
    </script>
</head>
<body>
   <div id="tabs">
        <ul>
          <li><a href="#tabA1"><span>InnerTabA1</span></a></li>
          <li><a href="#tabA2"><span>InnerTabA2</span></a></li>
        </ul>
        <div id="tabA1"></div>
        <div id="tabA2"></div>
   </div>
</body>
</html>

InnerTab2.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
     <link type="text/css" href="css/jquery-ui-1.7.2.custom.css"
rel="stylesheet" />
    <script type="text/javascript" src="http://jqueryui.com/latest/
jquery-1.3.2.js"></script>
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/
ui.core.js"></script>
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/
ui.tabs.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#tabs").tabs();
        });
    </script>
</head>
<body>
   <div id="tabs">
        <ul>
          <li><a href="#tabB1"><span>InnerTabB1</span></a></li>
          <li><a href="#tabB2"><span>InnerTabB2</span></a></li>
        </ul>
        <div id="tabB1"></div>
        <div id="tabB2"></div>
   </div>
</body>
</html>

Thanks in advance!

Adrian

+1  A: 

Once you called a innerTab page, all the HTML elements become rendered at once. You could be having issues with multiple copies of ID fields, such as "tabs"... The JQuery is referencing $("#tabs"), but there are two present.

Can you please post the JQuery error?

Anthony M. Powers
+1: element IDs must always be unique; otherwise you'll get very unpredictable behavior.
Matt Ball
Refer to Wally's answer (below) for how to fix your code as I explained, with a code sample. (Thanks Wally!)
Anthony M. Powers
A: 

If you are fetching the inner tabs through ajax, don't include a full html document and jquery, everything is being redefined in javascript and is conflicting.

Simply remove the whole head, body, and html wrapper tags leaving the , and it should work.

You also need to redefine the tabs when they it is loaded. Add a callback to the ajax tabs function -- (something like function ontabAJAX(){ if ($('.tab .active .innertabs').length)){ $('.tab .active .innertabs').removeClass('innertabs').addClass('tabbed').tabs() }).

CodeJoust
+2  A: 

You are definitely running into a little bit of trouble with your duplicated 'Tabs' ID attribute. The following example should achieve what you're looking for:
Main File (Outer Tab)

<html>
<head id="Head1" runat="server">
    <title></title>
    <link type="text/css" href="http://static.jquery.com/ui/themes/base/ui.base.css"
      rel="stylesheet" />
    <link type='text/css' href='http://static.jquery.com/ui/themes/base/ui.tabs.css'
      rel='stylesheet'>
    <script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.tabs.js"&gt;&lt;/script&gt;

</head>
<body>
  <div id="OuterTab">
    <ul>
      <li><a href="#InnerTab1" title="InnerTab1"><span>InnerTab1</span></a></li>
      <li><a href="#InnerTab2" title="InnerTab2"><span>InnerTab2</span></a></li>
    </ul>
    <div id="InnerTab1">   
    </div>
    <div id="InnerTab2">
    </div>
  </div>
      <script type="text/javascript">
        $(function() {
            $("#OuterTab").tabs();
            $('#InnerTab1').load('tab1.html', function() {
              $('#tabs').tabs();
            });
            $('#InnerTab2').load('tab2.html', function() {
              $('#tabs2').tabs();
            });
        });
    </script>
</body>
</html>

Tab 1 (tab1.html)

<div id="tabs">
  <ul>
    <li><a href="#tabB1"><span>InnerTabB1</span></a></li>
    <li><a href="#tabB2"><span>InnerTabB2</span></a></li>
  </ul>
  <div id="tabB1">
    This is tab b1 content
  </div>
  <div id="tabB2">
    This is tab b2 content
  </div>
</div>

Tab 2 (tab2.html)

<div id="tabs2">
  <ul>
    <li><a href="#tabA1"><span>InnerTabA1</span></a></li>
    <li><a href="#tabA2"><span>InnerTabA2</span></a></li>
  </ul>
  <div id="tabA1">
    This is tab A1 content
  </div>
  <div id="tabA2">
    This is tab A2 content
  </div>
</div>

This way, when you run your main file, the two tab files are requested via jQuery and loaded into the appropriate divs. Then I have defined a callback to activate the tabs that were just loaded.

If you need more details just follow up with a comment.

Wally Lawless
WallyThanks for helping out. Your solution works great, but I've got a couple of further questions. Is it possible to lazy load any non-visible external files (i.e. tab2.html) so that they are only accessed when their outer tab is clicked? Also, is it possible to use the external files directly, i.e http://localhost/tab1.html). I'm basically trying to 're-use' the external files as part of the outer tab page, but I need to access them directly too. Thanks againAdrian
Adrian Pritchard
Lazy loading the files wouldn't be too much of a stretch, you would have to attach the .load() functions to the parent tab's click() event. For your second part, you would have to use a partial content selector when getting the tab1.html and tab2.html files. If my example files were full HTML pages (<html><body> etc.) you could use this instead: $('#InnerTab2').load('tab2.html #tabs2', function()... instead.Make sense?
Wally Lawless
WallySorry it's taken a while to respond. Been a bit sidetracked lately on other stuff. I hope to get back to my jQuery development soon, and try out your suggestions.Adrian
Adrian Pritchard
No worries, I know the feeling!
Wally Lawless