Hide Show DIV Content with Links by using Javascript

The Hide and Show DIV elements for HTML content is a great way to clean up your sidebar. It becomes especially handy for those long blogroll lists or anything that clutters up the blog. Just simply click a link to make all the hidden content magically appear and disappear.


Demo:

+/- Click Here to Expand and Collapse

How to use it in Your Blog

Step 1: Adding CSS and Javascript
First, add this code below directly above the </head> tag.
<style type="text/css">
.commenthidden {display:none}
.commentshown {display:inline}
</style>
<script type="text/Javascript">
function togglecomments (postid) {
var whichpost = document.getElementById(postid);
if (whichpost.className=="commentshown") { whichpost.className="commenthidden"; } else { whichpost.className="commentshown"; }
} </script>

Step 2: Adding the Active Content to be Displayed
Place this code wherever you would like it to appear. Make sure to change the red text to a name that is "unique" to any other name in your template. An example might be "blogroll-dropdown". If you do not give each Hide Show DIV element a unique name the script cannot be used more than once on the same page. The green text is the title of the link that you click on to make the content appear. Replace the code "Expanded Content" with the content you wish to have expand and collapse.
<a aiotitle="click to expand" href="javascript:togglecomments('UniqueName')"
>Link Title</a><br />
<div class="commenthidden" id="UniqueName"
>Expanded Content</div>

Step 3: Sample Code
Here is what my code looks like for the demo above:
<a aiotitle="" href="javascript:togglecomments('hideshowdemo')">+/- Click Here to Expand and Collapse</a>
<div class="commenthidden" id="hideshowdemo"><ul><li><a href="http://howhero.com/">HowHero Tutorials</a></li><li><a href="http://widgetsforfree.blogspot.com/2008/09/translate-blog-widget-using-flags.html">Blog Translation Plugin</a></li><li><a href="http://widgetsforfree.blogspot.com/2008/09/make-money-blog-widget-performancing.html">Make Money Widget</a></li></ul>
</div>

51 comments:

Mesothelioma Helper said...

John, this is really great because now I can fit more stuff on my sidebars. I prefer using this over you drop down menu.

widgetblogger.co.cc said...

thanx so much
would use ut soon on my blog

Helen said...

Thanks for this tip, I will definitely use it in my next pet project :)

Evans said...

Wow, you're a genius - nice tip that deserves to be bookmarked.

I will link to it from my blog.
Thanks for sharing :)

Vicky said...

That's a great post! btw do you have any idea if I were to use it more than one in a page? Thanks..

John Smith said...

Vicky, Vicky if you were to use this Hide Show DIV element more than once you need to make sure to give each element a unique name (as described in the red text in the post).

Evans said...

This is one nice tip for adding valued interactivity to one's site. It will definitely be on show on my blog.

Thanks for sharing :)

Anonymous said...

Thanx for this. I want to use this expand hide
feature to hide and show some pics and then link those pics to some other pages.

my question is if i do that for like 10 pictures do i have to repeat this code for each button? will it make the page slow? pls HELP anyone??

Kaixi said...

Cool, I'm not really into Javascript, so that was a great tip for me.

cryoffalcon said...

Hi to expand this you have to click on it, can you make a automatic opening one, i have seen it on sites, and one more thing i want to use it for the labels in sidebar, can you give me the code how to use this gadget there.
Thanks for reading.
Regards
Shah Faisal

swEet zirCon said...

I can't find the exact tag to be replace with the new one. Is there any different tag? Please let me know coz i'm not familiar with the html code. TQ

Ajin Jude said...

Hi, Can you tell me how I can make this expand and collapse on hover rather than click?

fazrul arhan said...

This is great! i'm using this tweak now on one of my blogs..it's reduced space..i always like this tweak since i play friendster last time,haha..thank u so much John!!

ferux said...

Thanks so much. It was helpful

Peter Boomsma said...

I've been looking for a method to just show one div at the time. I want to have multiple popup divs, but only one at the time.

So if you have 3 links,
link1
link2
link3

They all open a different div but i want to close the div of link1 when a viewer clicks on link 2. So he does not have to close link1 before openening link 2.

Anyone?

Khmer Spy said...

Thx a lot for this. M using it here: http://kh-spy.blogspot.com/

IsHu said...

will this concept work under post ??

Twitter Buzz said...

Auto Tweet Generator Software!

Autopilot Twitter Marketing Just Through One Software!

Tweeting When You're Away From Your Computer Has Never Been Easier!

"Discover How This Twitter Schedule Tweets For You On Autopilot Mode"

Sit Back And Relax And Give Your Twitter Followers Valuable Content
While You're Away.... That'll Turn Them Into Raving Fans..

-> Manage your Twitter postings and schedule multiple posts in just seconds easily!

-> Set the time, edit and import messages in just a few clicks of a button!

-> Think about the time saved on making changes without having to log into Twitter!

-> Twitter marketing made easy because you can track your tweets through one software!

-> Gain rapid exposure through Twitter without having to physically be involved in tweeting all day!

-> It automatically submits your messages to Twitter while you focus on more pressing matters!

If You Can Click, Copy, and Paste, You Can Use "TwitterBuzz Auto Tweet Generator!

So go ahead! and Click Here to Download Your Software immediately...





Auto Tweeter Bot

Tweet Automator

Automated Twitter Tool

An Automated Tweeting Solution

Automatic Tweet

Defensive Pro said...

can v make it expand by default?

Anonymous said...

i want to display more less option using javascript div container doesn't contain any data means more option is automatically disappear

Anonymous said...

i want to set more/less option using javascript using usercontrol and div container. more option automatically disappear while at the time of div container empty

Bragadeesh said...

Cool stuff!

movies said...

THANK U http://movsshare.blogspot.com/

Igor Sousa said...
This comment has been removed by the author.
Anonymous said...

Thank you..i save lots of time

2t@ said...

tanks i use it in my blog www.templates2day.blogspot.com

javieth said...

JavaScript is a good program and very easy to use. I don´t like a complex program. I prefer javascript because i consider it like a device very eficient and it have a good quality.
I always looking for the quality that is why i prefer to buy viagra because i always have a great result in my sexual life.

hard drive recovery said...

very good post and very new information for me. I must try this thing on my blog hard drive recovery blog

Ankara Src said...

its fantastic post about this.thank you very much.

ISO 9001 said...

thank u very much for this comment.

ISO 9001 said...

thank u very much for this comment.

Gelinlik said...

perfect share for widget.

Harvest Türkiye said...

thank you very much for your share.

Rejeneratif Tıp said...

thank you very much for your share...

Antalya Araba Kiralama said...

Thank you very much,perfect

Gelinlik said...

Gelinlik

Platelet Rich Plazma said...

http://www.harvestturkey.com thank you very much

Kök Hücre said...

Platelet Rich Plazma thank you very much

Sawah Egy said...

http://www.sawah-egy.blogspot.com

thank you

Vefat ilanı said...

widget.Thank you very much

maybornstar said...

Not working in Firefox 3.6.17

essay jobs said...

thanks a lot))) very helpful post! appreciate it)

term paper writing service said...

very iteresting psot! thanks a lot

gelinlik said...

wonderful.this is very interesting.Thank you very much for this post

Anonymous said...

demo doesn't work!
not in IE9 or Chrome

essay example said...

Thanks for sharing such an interesting information. I think this is really a very nice post. Thanks for the great content!

buy essay online said...

Thanks for the article. Very interesting.

Anonymous said...

http://top-answers.net/webdesign/html-onclick-expansion-javascript.html

order research paper writing said...

Thanks for giving article, the guide line you provided about how to add css and java script in our blog was superb, its bit difficult to understand in one time but i am trying to learn.

Matt Muji said...

thanks for sharing..

eksis dengan internet

college book reports said...

thanks for providing so many things related to java script i enjoyed allot while reading this article, there are so many ideas hidden in this article.

Post a Comment