views:

67

answers:

1

I am going to have an ExpressionEngine weblog that will place user designed content blocks in the footer. But, it's only going to show one at at time.

My HTML looks like this:

<ul class="footernav">
    <li class="first"><a class="active" href="#">Get in touch</a></li>
    <li><a href="#">Interested in joining?</a></li>
    <li><a href="#">Feedback</a></li>
    <li><a href="#">Link 4</a></li>
</ul>
<div class="copy gutters show">
    <p>Lorem Ipsum</p>
</div>
<div class="copy gutters hide">
    <p>Lorem Ipsum</p>
</div>
<div class="copy gutters hide">
    <p>Lorem Ipsum</p>
</div>

I want to switch the show class to a hide class depending on the clicked link. Not sure how I can accomplish this. It has to be flexible enough to work with N number of content blocks--because they will be defined by the user in ExpressionEngine.

I'm pretty much brand new to JavaScript so I would really appreciate any insight, or solution for how I might accomplish this.

+3  A: 

I think this would work with your structure:

// Cycle through each link in our ul.footernav element
$(".footernav a").each(function(i,o){
  // Attach click-logic to each link
  $(this).click(function(e){
    // Prevent page from bouncing, reloading, etc.
    e.preventDefault();
    // Add .active class to this, but remove from all other links
    $(this).addClass("active").closest("li").siblings("li").find("a").removeClass("active");
    // Show any DIV having class .copy and same index as clicked link
    // meaning, clicking the second link will show the second div
    $("div.copy:eq("+i+")").show().siblings("div.copy").hide();
  });
});

Demo Online: http://jsbin.com/ekecu

Jonathan Sampson
Thanks for the help! This doesn't work just yet, but I'm starting to see it. Doesn't there need to be some sort of specific identifier to tell it which link should show which content block? Any solution for how I might do that?
Fuego DeBassi
unknown, the "identifier" is the order in which the links appear. The first link will toggle the first div. The second link will toggle the second div, etc.This works - check the "Demo Online" at the bottom of the solution.
Jonathan Sampson
Hmm. Quite right it does seem to work exactly as needed on your JsBin example. However the exact snippet your using successfully there, works oddly when I implement (it's a local EE install, or I would link it). It seems to only work on the last link in the list. In the case of our example, Link 4. Once the last link has been activated the one immediately preceding it activates the first content block. Any thoughts?
Fuego DeBassi
Banderdash, you don't have any other `div.copy` elements, do you? I'm assuming your HTML example here accurately represents the one you have on your machine.
Jonathan Sampson
Nevermind. I forgot I was using the div.copy on other areas of the page as well, was reaking havoc. Thank you very much Jonathan. Only other question is the active class on the <a>...doesn't switch with the clicks.. Any thoughts?
Fuego DeBassi
Or rather it add's the active class fine to which new link is clicked, but doesn't remove it from the others.
Fuego DeBassi
I've edited your script on JsBin to show the problem I am having. Click through some of them here: http://jsbin.com/eyaso
Fuego DeBassi
@Banderdash I've updated my solution. I've changed `.siblings("li a")` to `.siblings("li").find("a")` to fix the problem.
Jonathan Sampson
Awesome. Problem solved. Thanks so much!
Fuego DeBassi
You're welcome, Banderdash. Welcome to StackOverflow!
Jonathan Sampson