MohammedRabee Alkharroubi MohammedRabee Alkharroubi Author
Title: How to Reveal the Contents using JavaScript?
Author: MohammedRabee Alkharroubi
Rating 5 of 5 Des:
If you wanted to show/hide(reveal) the contents in HTML document by using simple methods, it is possible by using a simple javascript code r...
If you wanted to show/hide(reveal) the contents in HTML document by using simple methods, it is possible by using a simple javascript code running in it. This feature not only helps to make your webpage more stylist and attractive, also helps to present main contents or headings for the first look. It also helps to minimize the space for webpage which have very long content.

In this post, I am going to describe "How to Reveal the Contents using simple JavaScrpt code". Here I am using simple CSS code to set the CSS property of HTML element show or hidden and executed a JavaScript function on window.onload event handler.

You can use the following simple CSS code to set the CSS property of HTML element show or hidden.

.reveal * {display:none;}

 // This code specifies the display property of HTML element having class name "reveal" to none

.reveal *.handle{display:block;}

// This code specifies the display property of HTML element having class name "handle" for child of "reveal" class to block.


You can use the following simple JavaScript function to Reveal the Contents.

window.onload=function(){

var elements=document.getElementsByClassName("reveal");
for(var i=0; i<elements.length; i++) {
var elt=elements[i];
//find the "handle" element with the container
var title=elt.getElementsByClassName("handle")[0];
//when that element is clicked, reveal the rest of the content
title.onclick=function(){
if(elt.className=="reveal") {elt.className="revealed"; title.innerHTML="Hide Contents";}
else if (elt.className=="revealed") {elt.className="reveal"; title.innerHTML="Show Contents";}
}
}
};

Here document.getElementsByClassName("reveal"); finds all container elements with class "reveal" and uses a simple for loop to assign each elements of "reveal" class to elt and assign first element of "handle" to title variable. At last CSS property of the elements are changed by assigning a function on onclick event.


Full HTML Code with JavaScript to Reveal the Contents


<html>
<head>
<title>How to Reveal the Contents Using JavaScript</title>
<style>
.reveal * {display:none;}
.reveal *.handle{display:block;} /*Except for the class "handle" child */
</style>
<script>
window.onload=function(){
//find all container elements with class "reveal".

var elements=document.getElementsByClassName("reveal");
for(var i=0; i<elements.length; i++) {
var elt=elements[i];
//find the "handle" element with the container
var title=elt.getElementsByClassName("handle")[0];
//when that element is clicked, reveal the rest of the content
title.onclick=function(){
if(elt.className=="reveal") {elt.className="revealed"; title.innerHTML="Hide Contents";}
else if (elt.className=="revealed") {elt.className="reveal"; title.innerHTML="Show Contents";}
}
}
};
</script>
</head>
<body>
<div class="reveal">
<h3 class="handle">Show Contents</h3>
<h4 class="handle">Click Here to Reveal Hidden text</h4>
<p> This paragraph is hidden. It appears when you click on the title.</p>
<h4 class="handle">Click Here to Reveal Hidden text</h4>
<p> This paragraph is hidden. It appears when you click on the title.</p>
<p> This paragraph is hidden. It appears when you click on the title.</p>
<h4 class="handle">Click Here to Reveal Hidden text</h4>
<p> This paragraph is hidden. It appears when you click on the title.</p>
<p> This paragraph is hidden. It appears when you click on the title.</p>
<p> This paragraph is hidden. It appears when you click on the title.</p>
<p> This paragraph is hidden. It appears when you click on the title.</p>
</div>
</body>
</html>


Preview of Above HTML Code to Reveal the Contents


Show Contents

Click Here to Reveal Hidden text

This paragraph is hidden. It appears when you click on the title.

Click Here to Reveal Hidden text

This paragraph is hidden. It appears when you click on the title.
This paragraph is hidden. It appears when you click on the title.

Click Here to Reveal Hidden text

This paragraph is hidden. It appears when you click on the title.
This paragraph is hidden. It appears when you click on the title.
This paragraph is hidden. It appears when you click on the title.
This paragraph is hidden. It appears when you click on the title.

You can also use Mouse over and Mouse out event to generate instant preview of the contents for the given headings for the above function as follows.

title.onmouseover=function()
{elt.className="revealed";}

title.onmouseout=function()
{elt.className="reveal";}

Here is a full HTML code for the above JavaScript code.

<html>
<head>
<title>How to Reveal the Contents Using JavaScript</title>
<style>
.reveal * {display:none;}
.reveal *.handle{display:block;}
</style>
<script>
window.onload=function(){
var elements=document.getElementsByClassName("reveal");
for(var i=0; i<elements.length; i++) {
var elt=elements[i];
//find the "handle" element with the container
var title=elt.getElementsByClassName("handle")[0];
//when that element is clicked, reveal the rest of the content
title.onmouseover=function()
{elt.className="revealed";}
title.onmouseout=function()
{elt.className="reveal";}
}
};
</script>
</head>
<body>
<div class="reveal">
<h4 class="handle">Mouse over to Reveal Hidden text</h4>
<p> This paragraph is hidden. It appears when you mouse over on the title.</p>
<p> This paragraph is hidden. It appears when you mouse over on the title.</p>
<p> This paragraph is hidden. It appears when you mouse over on the title.</p>
<p> This paragraph is hidden. It appears when you mouse over on the title.</p>
</div>
</body>
</html>


Preview of Above HTML Code to Reveal the Contents


Mouse Over Here to Reveal Hidden text

This paragraph is hidden. It appears when you mouse over on the title.
This paragraph is hidden. It appears when you mouse over on the title.
This paragraph is hidden. It appears when you mouse over on the title.
This paragraph is hidden. It appears when you mouse over on the title.


Related Posts:

Advertisement

Post a Comment

 
Top