16

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>

Subscribe for Free Updates:

16 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/

Post a Comment