1

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

2
  • 2
    Here ya go. tinyurl.com/2c3vdba Commented Jul 21, 2010 at 3:29
  • Ken's on the right track. I basically need to have a bunch of links and when you click the link, the content loads to the right and hides the previously loaded content. Commented Jul 21, 2010 at 14:23

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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
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
$(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 }); });
Got it working pretty well 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
this is where im at... almost there 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?
|
1

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(); }); 

1 Comment

+1 Interesting approach. This question is like a blank slate.
-1

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.

2 Comments

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.
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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.