How to create a spoiler or show-hide effect
Here is a very simple show/hide effect code for you to copy and use. This effect is also known as a spoiler or a peek-a-boo. It uses a small inline Javascript. You can use this code anywhere, be it in the sidebar or in your blog post, as long as you can add the spoiler code to it.
[Update Mar 2013] See our new and improved content spoiler.
This code will add a button, clicking it will expand the content, showing the hidden part. See it in action, click the button now,
Simply put the content you want hidden inside the division tags, replacing the phrase HIDDEN CONTENT HERE
.
VISIBLE CONTENT HERE <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'}">Show/hide</button>
Customization
- You can replace “Show/hide” text with your own, in line 5.
- If you want to have multiple spoilers on a single page, make sure you give each of them a unique ID.
- To use a regular link instead of a button, simply replace the word
button
with a lettera
.
Enjoy!
59 comments to "How to create a spoiler or show-hide effect"
Really useful. Thanks to share a very informative post.
Thanks for sharing. :)
Nice Javascript. I like your every coding :)
@Anup
Thanks
I feel you should point out that if you're trying to do this on a main page then the code will only call for the first instance of "Spoiler". If you want to have multiple expanding posts on a single page you need to make sure each of them has a unique ID.
Other than that, though, thanks a bunch. Certainly the easiest implementation of expanding posts I've been able to find.
Anyway to get this to work multiple times on the same page?
@AiresOFwar
Like DRM commented above, you have to use different IDs. For each spoiler, replace the ID "spoiler" in line 2 and line 5 (3 places in line 5) with a new ID.
I think buttons are pretty ugly. What script replaces the button with a simple link?
@Anonymous
To turn the button into a link, replace the button tag in line 5 with an a tag -[button and [/button] with [a and [/a] respectively.
um where exactly do you put this in your code? I've tried it on my blog and the button is not appearing. sorry if this is a noob question.
@lisab
You put it in the post.
First type your post, then switch to HTML mode to insert the code.
great
Is there a way to replace the button image with my own image?
@erin
Replace the code in line 5 with this one:
<img src="YOUR_IMAGE_URL" title="Click to show/hide content" onclick="if(document.getElementById('spoiler') .style.display=='none') {document.getElementById('spoiler') .style.display=''}else{document.getElementById('spoiler') .style.display='none'}/>
YOUR_IMAGE_URL is the direct link to your image.
It doesn't work. Only the image shows :(
@erin
Oh I forgot the closing quotation mark for onclick content. Use this instead:
[img src="YOUR_IMAGE_URL" title="Click to show/hide content" onclick="if(document.getElementById('spoiler') .style.display=='none') {document.getElementById('spoiler') .style.display=''}else{document.getElementById('spoiler') .style.display='none'}"/]
replace [ with < and ] with >
When I put your code in (with a small mod), it works, but with two issues:
1. It only shows the expanded section for less than 2 seconds
2. When I tried to change the buttons tags to simple [a] tags, it worked fine from a display perspective, but blogger keeps putting a default url in (for my page edit) and I don't want the url to go anywhere, but to simply show the hidden section.
Here's the modified code:
[a href="" onclick="if(document.getElementById('spoiler') .style.display=='none') {document.getElementById('spoiler') .style.display=''}else{document.getElementById('spoiler') .style.display='none'}" title="Click to show/hide content" type="button"]THIS IS THE TITLE FOR ARTICLE 1[/a]
[div id="spoiler" style="display: none;"]
This is the hidden section for article 1 by default.[/div]
The end result of that code is that is shows "This is the title for Article 1" as a link, but within 2 seconds, it redirects me to the edit page of blogger, so basically, I'm back to the design aspect and it's showing me the code again.
Suggestions?
@:: House ::
Replace the a tags with span tags and remove href attribute, like this:
[span onclick="if(document.getElementById('article1') .style.display=='none') {document.getElementById('article1') .style.display=''}else{document.getElementById('article1') .style.display='none'}" title="Click to show/hide content" style="cursor: pointer;"]23-february-2011 - Conference Tournaments[/span]
@:: House ::
...and you might want to add text color into the style attribute (to mimic link):
style="cursor: pointer; color:#cc0000"
Thanks. I changed the a tags to span and it works as you suggested. Thank you very much. I thought there was going to be a need to update the overall site xml template.
Appreciate the help.
Green, i could not copy the whole code because the edge part of the fifth line is hidden below your sidebar (the line goes to far to the right), it's shown like this: document.getElementById('sp.....(i can't see the rest)
@Azmee @ Buka-rahasia Perjalanan Blogger
That happens when the page is not fully loaded. Try refreshing. You should see the code in black and blue texts.
Thank you, it is helpful
but what if have to use it more than 1 in same post?
@Anonymous
Use different IDs. See reply #7
Hi there, I tried using the code and in the preview everything is how it should be - the text is hidden, the button has changed into a link that says what I want it to - except that the button/link doesn't seem to realise it's supposed to do something. I click on it and nothing happens! I haven't published yet because I want to get it right, so this is happening in the preview. Is this the reason? Will it show up properly when I publish the post?
@Zora_Aisling
Yes it will be clickable once you publish it.
So it is! Thank you @Greenlava
thank you for this tutorial! :)
Thanks for your guide for show/hidden button post.
I applied to my blog, good result.
But i want to make more buttons in my post, its like:
SHOW/HIDDEN BUTTON 1
Text 1 that hidden before was showed.
Read more:
SHOW/HIDDEN BUTTON 2
Text 1 that hidden before showed.
Unfortunately, problem happended. After I click 'SHOW/HIDDEN BUTTON 1', and click 'SHOW/HIDDEN BUTTON 2', its hide the text 1, but not open text 2.
I dont know where the problem is.
My url for this post blog for your review, as follow:
http://semuadasini.blogspot.com/2011/05/klik-kedai-edymart.html
TQ again.
@semuadasini
To create more than one spoilers, you must give the spoilers a unique ID. For example you can use "spoiler2" as the ID for the second spoiler. You need to replace "spoiler" with "spoiler2" at all four places -one in line 2 and three in line 5.
Greenlava, thanks a lot for this tips.
it's really useful and easily understand.
really thank youu!!
I tried using the code and it only hid the first line of the text I want to hide, even though I put all of it in the "hidden content here" area. Any suggestions?
@Alison
What's the URL of your post?
thanks dude...
Thanks... ^^
Can we put button top of the hidden content instead of bottom of content ?
@Bali Land & Villa
Yes you can.
Thanks so much! This has helped massively with my new music composition and piano technique blog, particularly having different levels of difficulty all in the same post and keeping it tidy.
so useful thankyou ~!
Thanks.. It was a simple tutorial. Much better than others. Others are just too complicated. Thanks again bro.
Yoy are the master, thank you sooo much!
really helpfull, thank's
It's very useful. Thank you!
Thank you! I have been looking, for the longest time, for a this particular code with the easy to follow instructions. It works very well on my website. :)
Greenlava, this is awesome! Thanks so much! Just one question? What code can I add to change the button color and the text color? Many thanks!
@C.E.R
Insert a style attribute into the button tag, like this:
<button style='background-color:PUT_COLOR_CODE_HERE; color:PUT_COLOR_CODE_HERE; title="Click to show/hide ...
Thanks for the awesome tutorial Greenlava! This worked really great!
But I have one question. What if I want to change the name of the spoiler button when the content is shown like when the button is clicked and the content appears, the button will change its name to "Hide Content" and when you click the button again to hide the content, the button name will go back to "Show Content" (if that made sense, sorry I suck at explaining) how do I do that?
@Altoseer
I'm working on an improved version. Will release it in a new post soon.
Awesome! I have just one question, how to move the button to the right?
@heX-
Replace line 5 with this:
<button style="float:right;" 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'}">Show/hide</button><br/>
I used the code and its working fine as button but when i use it link it works but when i click on the link it shows the hidden text and the page got refresh.
please help.
Hey, I can't used this on a multiple page. I want to make a multiple link having different
spoiler on each of it. can you help me? Also, I'm making it consecutive but only one
is working. email me please here lovely.tamahome@yahoo.com. Thanks. :))
@Anz Joy
Add this style tag (see reply 51) in line 5 with:
style="background: orange; border: 1px solid black;"
@Grace
Use our new and improved content spoiler
Hi, is it possible to make the button stay at it's place after clicking? I want hidden text to show under the button, not above.
thanks, this is super helpful
Working perfectly..... but please make sure if you hide text i should not be hidden to google or other search engines too...
admin ===> Working perfectly..... but please make sure if you hide text i should not be hidden to google or other search engines too...
THANK YOUUUUUUUUUUUUUUUUUU!!!!!
Incidentally, I was looking for this trick .. Thank you for the tutorial ..
Comments on this post are closed.