When I place them inside of multiple divs, it does not seem to work any longer. What am I missing? Thanks
Demo: http://jsfiddle.net/HVfaA/206/
<div id="content">
<div id="first">
<div id="first-1">
<div id="first-2">
<div id="first-3">
<li><a href="#" class="one">One</a></li>
<li><a href="#" class="two">Two</a></li>
<li><a href="#" class="three">Three</a></li>
<li><a href="#" class="four">Four</a></li>
</div>
</div>
</div>
</div>
<div id="second">
<div id="second-1">
<div id="second-2">
<div id="second-3">
<div id="one">One</div>
<div id="two">Two</div>
<div id="three">Three</div>
<div id="four">Four</div>
</div>
</div>
</div>
</div>
</div>
$("div#second > div").hide();
var divs = $("#one, #two, #three, #four");
$('li a').on('click', function () {
$(divs).hide();
$("#" + $(this).attr("class")).show();
});
This function is a modified version of the one offered by Peter Ajtai jQuery show div on click, hide the others
Original question below:
I was trying use a separate divs containing the links and to show the divs.
Currently When the links are clicked, the hidden divs do not show. Thanks for your help.
Here’s the demo on the jsfiddle – http://jsfiddle.net/HVfaA/184/
<div id="first">
<li><a href="#" class="one">One</a></li>
<li><a href="#" class="two">Two</a></li>
<li><a href="#" class="three">Three</a></li>
<li><a href="#" class="four">Four</a></li>
</div>
<br /><br />
<div id="second">
<div id="one">One</div>
<div id="two">Two</div>
<div id="three">Three</div>
<div id="four">Four</div><br/><br/>
</div>
// Optional code to hide all divs
$("div#second").hide();
// Selector
var divs = $("#one, #two, #three, #four");
// Show chosen div, and hide all others
$("li a").click(function ()
{
$("#" + $(this).attr("class")).show();
divs.not(("#" + $(this).attr("class"))).hide();
});
The issue is that you’ve wrapped all your divs in
div#secondand then hidden that element. Because their parent is hidden, none of the child divs (#one, #two, #three, #four) will show up.If you’d like to hide all divs to begin with try
This isn’t directly related, but as an aside you can leave off your
divs.not(("#" + $(this).attr("class"))).hide();by just shifting the order of when you hide your divs (hide them all, then show the one). Also, since you already stored them asdivsyou can also just reference them directly.See updated Fiddle http://jsfiddle.net/HVfaA/190/