Question

Here's my code , I would like to basically have upper parents tabs , but once you click on a tab , there will be sub-tabs in the clicked tabs so that you can view different contents of the different tabs within the parent tab .. When I click on the different parent tabs , they show , but when I click on the subtabs , the subtabs menu dissapear and I don't see anything .. can anyone tell me where I went wrong ?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>

<script type="text/javascript">

  jQuery(document).ready(function() {

    $('#tabs > div').hide();
      $('#tabs div:first').fadeIn('slow');
      $('#tabs ul li:first').addClass('active');
      $('#tabs ul li a').click(function(){
                $('#tabs ul li.active').removeClass('active');  // <== Only what you need
                $(this).parent().addClass('active');
                var selectedTab=$(this).attr('href');
                $('#tabs > div').fadeOut('slow', function() {       // <== Use a callback
                    $(selectedTab).delay(10).fadeIn('fast');          // <== add a delay
                });        
                return false;
            });

      // Video Feeds subcategories 
      $('#tabs-video-feeds > div').hide();
      $('#tabs-video-feeds div:first').fadeIn('slow');
      $('#tabs-video-feeds ul li:first').addClass('active');
      $('#tabs-video-feeds ul li a').click(function(){
                $('#tabs-video-feeds ul li.active').removeClass('active');  // <== Only what you need
                $(this).parent().addClass('active');
                var selectedTab=$(this).attr('href');
                $('#tabs-video-feeds > div').fadeOut('slow', function() {       // <== Use a callback
                    $(selectedTab).delay(10).fadeIn('fast');          // <== add a delay
                });        
                return false;
            });

  });

</script>
</head>

<body>

    <div id="tabs">
      <ul>
        <li><a href="#tabs-1">Public Figures</a></li>
        <li><a href="#tabs-2"> My Feeds </a></li>
        <li><a href="#tabs-3">Videos</a></li>
        <li><a href="#tabs-4"> Music </a></li>
        <li><a href="#tabs-5">Pictures</a></li>
        <li><a href="#tabs-6"> Infographics </a></li>
      </ul>

      <!-- Start of First tab -->

      <div id="tabs-1">

       First Parent tab

     </div>

     <!-- Start of Second tab -->
     <div id="tabs-2">

      Second parent tab content

    </div>  <!-- end of second tab -->

    <div id="tabs-3">

     Third parent tab .. Below are 3 subtabs .. the subtabs menu and the content ..

     <div id="tabs-video-feeds">
       <ul>
        <li><a href="#tabs-10"> Comedy</a></li>
        <li><a href="#tabs-11"> Music </a></li>
        <li><a href="#tabs-12"> Politics </a></li>
      </ul>
      <div id="tabs-10"> Comedy ..</div>
      <div id="tabs-11"> Music ..</div>
      <div id="tabs-12"> Comedy..</div> 
    </div>
  </div> -- end of third parent tab

  <div id="tabs-4"> 4 parent tab content </div>
  <div id="tabs-5"> 5 parent tab content</div>
  <div id="tabs-6"> 6 parent tab content </div>

    </div> <!-- End div of tabs -->
</body>
</html>
Was it helpful?

Solution

The problem is on the line

$('#tabs ul li a').click(function(){

which targets both parent and child tabs, instead of only parent ones. When the click on child tab occurs, fadeOut is done for all #tabs > div elements, effectively hiding all parent tabs with all their content.

Luckily fix seems to be pretty easy:

$('#tabs > ul li a').click(function(){

Here is the working fiddle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top