I am trying to replace divs on click. For that , i am using following script
$(function(){
var $containers = $("#center > div").hide();
$('div span a').each(function(i,el){
var idx = i;
$(this).click(function(e){
var $target = $containers.filter(':eq(' + idx + ')');
// Fade out visible div
if($containers.filter(':visible').not($target).length){
$containers.filter(':visible').fadeOut();
}
// Fade in new div if not already showing
$target.not(':visible').fadeIn();
e.preventDefault();
})
})
});
html code is
<div>
<div>
<span><a href="#">Home</a></span>
<span><a href="#">About us</a></span>
<span><a href="#">Specifications</a></span>
<span><a href="#">Contact us</a></span>
</div>
<div id="center">
<div class="container">
Hey , this is Home div contents
</div>
<div class="container">
Hey , this is About us div contents
</div>
<div class="container">
Hey , this is Specifications div contents
</div>
<div class="container">
Hey , this is Contact us div contents
</div>
</div>
</div>
Here all divs are hidden initially. I want to display the 1st div and other divs on click accordingly
Please help,
Thanks
Use :first selector
Example :