Categories

Tags Cloud

RSS Feed

Subscribe to blog RSS Feed Subscribe to Blog's RSS Feed
APR
2010
14
0 Comments
353 Hits
Easy "Spoiler" Show/Hide Element Using JavaScript
Posted under: Tutorial
Tags: Scripting

Sometimes we want to hide some content at first page load to reduce bandwidth transferred to the user. Usually when we just want to show the thumbnails of the image and then the original (big) sized image hidden next to it.We could hide some element by using CSS display property set to "none" initially. In the example below, the element "spoiler" and it's content won't shows up on the browser:

<div id="spoiler" style="display:none">
  <img src="some-image.jpg" alt="One Big Image" /> Big Image!
</div>

To easily show/hide the element and it's content can be done, by executing the following javascript function:

<script type="text/javascript">
var show = function() {
  document.getElementById("spoiler").style.display = "block";
}
var hide = function() {
  document.getElementById("spoiler").style.display = "none";
}
</script>

Now, we may call the javascript functions by using the following anchor tag:

<a href="#" onclick="show()">Show!</a>
<a href="#" onclick="hide()">Hide!</a>

Have a good try!

Next
Prev

Write Your Comments

Comments are parsed with Markdown.
 
Notes
* Your email is required to submit this form, and it will not be published or shared without your consent. We use your email address to show your avatar picture profile from Gravatar. Don't have one? Then sign up to gravatar and create your own here.
We also filters your comment against SPAM because we hate SPAM as much as you do. If your comment is recognized as SPAM then it will be moderated, otherwise it will shows up immediately.
Form Key: #36295147ba234adb254a9b8f3ddad15d
Loading...