Translate This Blog
Remember To Always Back Up Your Template Before Attempting Any Of The Tweaks On This Site

Hide/Show Content Button

You can add a hide/show button to just about anything with just a few lines of  javascript. This is also known as a spoiler button. You can use this on text, images, javascript, and pretty much anything you can think of. Now Blogger has a Jump Break option in the post editor so its better to use that instead of this method below for post. This method below is more for gadgets which don't have the Jump Break option.

To use this just simply add this code where you want it.

<div id="spoiler" style="display:none">
HIDDEN CONTENT HERE
</div>
<button title="Click to show/hide content" type="button"
onclick="if(document.getElementById('spoiler')
.style.display=='none') {document.getElementById('spoiler')
.style.display=''}else{document.getElementById('spoiler')
.style.display='none'}">BUTTON TEXT</button> 

Just  replace the Blue text with the content or code you want to hide. Replace the  Red text with the name you want to display on the button. Now with the Orange text  you can leave it the same if you  plan to use this one time on a page. But if you want to use this multiple times on the same page you will need to change them each to a different name.   If you look in the code below you can see how I had to use spoilor1  instead to get it to display.


Here is a Example:



Now maybe you want the  content to display by default and only hide when clicked on. If that is the case use this code below.

<div id="spoiler1">
CONTENT TO HIDE
</div>
<button title="Click to show/hide content" type="button" onclick="if(document.
getElementById('spoiler1') .style.display=='') {document.getElementById('spoiler1') .style.display='none'}else{document.getElementById('spoiler1') .style.display=''}">BUTTON TEXT</button>

Example:

CONTENT TO HIDE


Now maybe you want something a little better the a simply gray button. Well you can use a image instead by replacing the BUTTON TEXT  with this code below.
<img src="IMAGE URL" />
Now just replace the blue text with the URL of the image you want to use.

5 comments:

Anonymous said...

Thanks so much! Now, is there a way to make several show/hide content buttons, but when one is shown and you want to show another one, the first hides itself automatically? Thanks! LUCAS.-

AiresOFwar said...

You need to enter a separate ID (orange) for the 2nd spoiler button.

TestSubjekt said...

I'm trying to use this for my video based blog to keep people from being overwhelmed by 10+ videos loading all at once.

But when I try the first method, which is what I'd prefer, the first video keeps repeating over and over again.

The second one works but again, I don't want the videos loading all at once. Is there something I'm doing wrong?

CMHE said...

Any tips on making this work in Internet Explorer?

admin said...

I've been looking for days for a hack that will (1) work and (2) not complicate my life. Others required me to do some tweaking on the main template as well. Yours just have to be appended on the page you where the hide/show functionality is needed.

Thanks so much for sharing. You just made my day.

Post a Comment

Please don't post hyperlinked text within the comment box! It will not be published and you will be marked as spam.

Web Design Blogs PageRank