tags:

views:

78

answers:

2

This code shows the primary div tag elements fine but ignores or does not recognize CSS and HTML in a child div tag.

I can get this code to open multiple divs but not the divs nested inside the first level of divs. 6 hours later I am hoping for outside help.

Here is the code:

<html>
<head>
<style type="text/css">
#tabs{
margin:20 px 0;
}
#tabs ul {
float: center;
background: #003333;
height:25px;
width: 500px;
padding-top: 4px;
}  
#tabs ul li {
float: left;
margin-left: 2em;
}
#tabs ul li a{
text-decoration:none;
color:#FFFFFF;
font-weight:bold;
}
#tabs ul li.active{
background-color:#669999;
}
#tabs div{
position:absolute;
width:500px;
height:500px;
padding:20px;
margin-top:2px;
}

#tab1{
background-color:green;
}

#tab2{
background-color:red;
}

#blueBox{
float:left;
width:100px;
height:100px;
background-color:yellow;
margin-left:5px;
margin-top:150px;
}

</style>

<script src="js/jquery-1.4.2.js" type="text/javascript" /></script>
<script type="text/javascript">
$(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').removeClass('active');
$(this).parent().addClass('active');
var selectedTab=$(this).attr('href');
$('#tabs div').fadeOut('slow');
$(selectedTab).fadeIn('slow');
return false;
});
});
</script>



</head>
<body>
<div id="tabs">
<ul>
<li><a href='#Tab1'>Tab 1</a></li>
<li><a href="#Tab2">Tab 2</a></li>
</ul>
<div id="Tab1">
<p>Contents in Tab1 </p>
</div>

<!--this next part is the part I can't get to work. The div blueBox inside the div        Tab2-->

<div id="Tab2">
<p>Contents in Tab2</p>

<div id="blueBox">
<p>Hello</p>
</div>

</div>

</div>

</body>
</html>

I believe it has something to do with not getting below the parent level to get the nested divs but haven't been able to google a solution despite an all nighter. This is a great little code but is not practical if I can't get the nested div tags to work.

Thanks in advance.

+1  A: 

Instead of hiding all descendant <div> elements, only hide the first level, so change this:

$('#tabs div').hide();

To this:

$('#tabs > div').hide();

this fixes the issue since it's never hidden in the first place :)

The same applies here:

$('#tabs div').fadeOut('slow');
//to..
$('#tabs > div').fadeOut('slow');
Nick Craver
Looks like OP is also affecting all descendants with the `fadeOut()` in addition to the `hide()`. ---- And then only the main tab `div` is faded back in but the children of the main tab remain invisible.
Peter Ajtai
@Peter - good catch, updated, didn't test that far :)
Nick Craver
+1  A: 

You only want to hide and show the direct child of #tabs. The other divs inside Tab1 and Tab2 should remain as they are. To accomplish this, use the direct child selector ( > ).

To simply get things working change the lines that use #tabs div to #tabs > div 2 lines:

    $('#tabs > div').hide();                         // <== direct child selector
    $('#tabs > div').fadeOut('slow');                // <== direct child selector

But there are a few other improvements.

  1. Changed $('#tabs ul li').removeClass('active'); to $('#tabs ul li.active').removeClass('active');, since it's a little more efficient to simple select the elements of interest with the active class instead of all the lis.

  2. Include the fadeIn in the callback of the fadeOut, this'll make things appear a little smoother, since it ensures that fadeIn only begins once fadeOut ends. In fact, the whole thing looks much nicer with a .delay() tacked on.

.

$(function() {                                   // <== shorter form of doc ready
    $('#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(500).fadeIn('slow');          // <== add a delay
        });        
        return false;
    });
});

jsFiddle example

Peter Ajtai
Thanks so much. The code works as intended now with your solution. Nothing better than a simple fix.
RightTurnClyde