tags:

views:

82

answers:

3

Hi im trying to figure out how to hide and show content using multiple links. Ex.

<a href="#">Content 1</a>
<a href="#">Content 2</a>
<a href="#">Content 3</a>

<div class="show">content #1</div>
<div class="hidden">content #2</div>
<div class="hidden">content #3</div>

So when someone clicks Content #2, it shows content #2 and hides content #1

A: 

Try .toggle() along with .hide()

http://api.jquery.com/toggle/

http://api.jquery.com/hide/

There are examples on those pages which cover what you want to do.

Ganesh Shankar
Hey Ganesh, I think toggle would work but I would need to toggle between multiple links and multiple different content blocks. The examples you provided uses the same link to hide and show one block of content.
Tom
You're right! Sorry I think I misunderstood your requirements slightly. In that case you'll need to write a bit of custom JS and using the .hide() method. Ken's answer is a pretty good place to start (you'll definitely need the IDs)
Ganesh Shankar
+2  A: 

Your links and divs have only the loosest of hooks on which to hang this sort of behavior. Perhaps you really do mean to associate links with their respective divs by ordinal position -- but if not, one way to start is by adding some meaningful ids. So:

<div id="linkarea">
    <a href="#" id="link-1">Content 1</a>
    <a href="#" id="link-2">Content 2</a>
</div>

and then

<div id="contentarea">
    <div id="c-1">content #1</div>
    <div id="c-2">content #2</div>
</div>

To make it work:

$('div#linkarea a').click( function(ev){
    ev.preventDefault(); // suppress natural click
    var idx = this.id.split('-')[1]; // grab the link "number"
    $('div#contentarea div[id=c-'+idx+']') // find respective div
        .show() // show it
        .siblings() // get its siblings
        .hide(); // and hide them
    });
});

Here's a working fiddle.

Ken Redler
Yah man, sorry im new to this whole game.How would i go about hiding Content 2 on load? I just want to show content 1 on load then when you click on the links it displays the appropriate content.That jfiddle is pretty sweet thanks for sharing
Tom
Ive been messing with this using the hide feature and I think I got it to work. Only thing is how do you prevent the initial load of the content? There is a second in there where the content shows so if I have a bunch of content it wont be the prettiest load
Tom
$(document).ready( function(){$('#c-2, #c-3').hide(); $('div#linkarea a').click( function(ev){ ev.preventDefault(); // suppress natural click var idx = this.id.split('-')[1]; // grab the link "number" $('div#contentarea div[id=c-'+idx+']') // find respective div .show() // show it .siblings() // get its siblings .hide(); // and hide them });});
Tom
Got it working pretty wellhttp://www.fredsparks.com/biographies/Only thing im trying to do now is add a class to the links to make them bold when you click. And remove when you click another. So the active link will be the only bold one out of the set
Tom
this is where im at... almost therehttp://jsfiddle.net/7hUhT/41/For some reason when you click on a link it doesn't apply the class. If you click it twice or even another link after the 1st click it then works.How do I get it to work on the first click?
Tom
@Tom, are you meaning to hide all content divs except the first on initial page load? You could simply set them as `display:none` in your style sheet, then `.show()` the first on load. As to the click problem, you're modifying the `onclick` *inside* the click function itself. So the new "bolding" behavior isn't attached until that first click. I'm changing your fiddle to fix these issues.
Ken Redler
Have a look at http://jsfiddle.net/7hUhT/44/
Ken Redler
Very nice Ken, this is exactly what I was looking to do.I'v learned a lot just from using the jsfiddle.com thanks for sharing.
Tom
Looking closer at it, for some reason now all the content shows on load. Im trying to only make the first block of content show on load
Tom
I think I figured it out, $('#c-2, #c-3').hide()Not pretty but it works. Im sure there is a way to do it with greater than but thats out of my league.
Tom
Tom, make sure you're properly identifying the content divs. My last fiddle mistakenly used a class instead of an id. Revised: http://jsfiddle.net/7hUhT/45/. i.e. `div#contentarea > div` in the css, instead of `div.contentarea > div`.
Ken Redler
+1  A: 

I would approach this in a slightly different way.

Instead of including the links in the HTML, generate them with javascript. This way, if someone has JS disabled, then they won't see the useless links.

<div title="Content 1">content #1</div>
<div title="Content 2">content #2</div>
<div title="Content 3">content #3</div>

Then the JS:

var $divs = $('div'); // or whatever selector is appropriate, maybe classes are needed?
var $linkDiv = $("<div></div>").insertBefore($divs);

$divs.each(function(index) {
    var $t = $(this);
    $("<a></a>", { href: '#', text: this.title })
        .click(function(e) {
            e.preventDefault();
            $t.toggle('slow');
        })
        .appendTo($linkDiv)
    ;
    this.removeAttribute('title');  // to avoid ugly hover tooltips
    if (index > 0) $t.hide();
});
nickf
+1 Interesting approach. This question is like a blank slate.
Ken Redler