0

Okay, here's my problem. I have a page where I use toggle to show/hide information about an article with a click on the name. And I have another page that use jQuery UI Tabs to display the same page but with different parameter (to be exact 3 tabs: order by Rate, by Date and by Name).

So everything working fine until I start to switch tabs and try to toggle element in this other tabs: - Sometimes everything works normal - Sometimes nothing happend. - Sometimes it toggle several times as if the script was loaded several times.

To be more precise here's my code:

Page where I display tabs:

    <link rel="stylesheet" href="/projectsoul/CSS/design/jquery.ui.all.css" />
    <link rel="stylesheet" href="/projectsoul/CSS/design/jquery-ui-1.8.21.custom.css" />
    <script type="text/javascript" src="/projectsoul/JS/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="/projectsoul/JS/jquery-1.7.2.js"></script>
    <script type="text/javascript" src="/projectsoul/JS/jquery-ui-1.8.21.custom.min.js">                       </script>

               ...
                 ...
                 ...
        <div id="content" class="content" class="ui-tabs">
        <ul>
            <li><a href="articles.php" ><span>By Date</span></a></li>
            <li><a href="articles.php?orderby=rate"><span>By Rate</span></a></li>
            <li><a href="articles.php?orderby=name"><span>By Name</span></a></li>
        </ul>
    </div>

     .... 
     ....
     ....


        $( "#content" ).tabs({
        ajaxOptions: {
            error: function( xhr, status, index, anchor ) {
                $( anchor.hash ).html(
                    "Fail to load content."
                     );
            }
        }
    });

    $( "#content").tabs({
            fx: { height: 'toggle', opacity: 'toggle', duration: 'slow' }
        });

    $('#content').tabs({
        load: function(event, ui) {
            $(ui.panel).delegate('.intabs', 'click', function(event) {
                $(ui.panel).load(this.href);
                event.preventDefault();
            });
        }
    });

And now, here is the page i display in these tabs:

    <link rel="stylesheet" href="/projectsoul/CSS/design/jquery.ui.all.css" />
<link rel="stylesheet" href="/projectsoul/CSS/design/jquery-ui-1.8.21.custom.css" />
<script type="text/javascript" src="/projectsoul/JS/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/projectsoul/JS/jquery-1.7.2.js"></script>
<script type="text/javascript" src="/projectsoul/JS/jquery-ui-1.8.21.custom.min.js"></script>

 <?php
while($articles= $articles_data->fetch())
{?>

 <h2><a class="toggler" id="displayText<?php echo $articles["id"] ?>"><?php echo nl2br(htmlspecialchars($articles["name"])); ?></a></h2>

  ... 
  ...

  <div class="toggle" id="displayText<?php echo $articles["id"] ?>_content">
 // CONTENT                 
  </div>


   } 
   // END OF WHILE

    <script>
    $(document).ready(function() {
       $('.toggle').hide();
       $('.toggler').click( function() {
            var target = this.id + '_content';
            // Use ID-selectors to toggle a single element.
            $('#' + target).toggle();
            // Use class-selectors to toggle groups of elements.
            $('.' + target).toggle();
            $('.toggle.always').toggle();
        });
     });
     </script>

As I said, the toggling work when the second page is displayed alone. But in tabs and after switching it doesn't work anymore.

I've searched but didn't find anything that seems to be like that... Any idea ?

I hope I made myself clear ^^"

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
Vesper Bled
  • 165
  • 1
  • 2
  • 10

2 Answers2

0

I'm not sure if there's a particular reason you're doing so, but loading jquery twice is generally not a good idea. Try limiting both pages to either jquery or jquery.min (the page in the iframe isn't affected by the parent page's script, so you shouldn't have to worry about it getting double jquery).

loading jquery twice causes an error?

Community
  • 1
  • 1
PeaBucket
  • 142
  • 1
  • 1
  • 9
  • I know but I need it. But even if I remove one loading, it still not working on some items. Really weird :/ – Vesper Bled Jul 10 '12 at 22:17
  • How are you including the page in the tabs? Unless you are using an iframe, all your scripts and css will carry into the tab pages (so you don't need to load any of the css or jquery files in the included page). Also, if parts of your page only work when you include jquery twice, there's a problem with your code (unless you made changes to one of the jquery files, in which case you need to rethink the changes) – PeaBucket Jul 10 '12 at 22:41
  • Yep, after some test, each times that I switch the tabs, the content is loaded again (even if the content are load in the first tabs), and that what makes the bug. Two options: Is there is a way to reload all tabs content when the switch is made or is there is a way to simply not load content already loaded ? ;) – Vesper Bled Jul 11 '12 at 06:13
0

Okay, problem resolved.

For those who have problem like that:

The problem mainly comes from the fact that content loads on each tabs and there is no distinct value in ID of toggled div between tabs. I just changed :

    <div class="toggle" id="displayText<?php echo $articles["id"] ?>_content">

into something like that:

   <div class="toggle" id="displayText<?php echo $articles["id"] ?>_<?php echo $current_tab_id ?>_content">

(of course $current_tab_id content the id of the tabs displayed)

Vesper Bled
  • 165
  • 1
  • 2
  • 10