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>
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>
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.
5 comments:
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.-
You need to enter a separate ID (orange) for the 2nd spoiler button.
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?
Any tips on making this work in Internet Explorer?
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.