Check out our newest creation Tutorial Grad. The automated tutorial directory.

Subscribe to our rss feed

Hide and Show a Div Using Javascript

Posted in HTML Tutorials, JavaScriptTutorials, Web Development Tutorials by John Ward on the July 27th, 2008

This tutorial will show you how to create a hidden Div and display it with the click of a link. There are a few reasons you may want to hide a Div in a website design. You may want to create a drop down type menu or a box that will show more information when you click a link. Another reason would be for SEO purposes. In theory hiding information in a Div is not against Google’s rules. As long as the user can still see the content when clicking a button you are not displaying different information to the user than you are to the search engine.

First we are going to start with a very basic page layout and create a 2 Divs. One will be shown and one will be hidden. Here is the starting code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hidden Div</title>
</head>

<body>
<div id="main">This is not hidden.</div>

<div id="hidden">This is hidden.</div>

</body>
</html>

Ok now I will add some basic style to these boxes.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hidden Div</title>

<style type="text/css">
<!--
#main{
	width:500px;
	height: 20px;
	background: lightblue;
}
#hidden {
	width:300px;
	height:20px;
	background: lightgrey;
}
-->
</style>

</head>

<body>
<div id="main">This is not hidden.</div>

<div id="hidden">This is hidden.</div>

</body>
</html>

The page should look like this so far.

Now to hide the div. To do this we will change the display to none in the CSS for the #hidden div. See the code below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hidden Div</title>

<style type="text/css">
<!--
#main{
	width:500px;
	height: 20px;
	background: lightblue;
}
#hidden {
	width:300px;
	height:20px;
	background: lightgrey;
	display: none;
}
-->
</style>

</head>

<body>
<div id="main">This is not hidden.</div>

<div id="hidden">This is hidden.</div>

</body>
</html>

Now when you preview the page you will not see the Div.

In order to show the div we will need to add a Javascript function. We will pass the ID attribute of the Div to the function. Basically this means that we can use this one function to show or hide more than one Div on the same page. Below is the Javascript code that we will add the the Head section of the page.

<script language="JavaScript">
	function toggle(id) {
		var state = document.getElementById(id).style.display;
			if (state == 'block') {
				document.getElementById(id).style.display = 'none';
			} else {
				document.getElementById(id).style.display = 'block';
			}
		}
</script>

The script above is creating the function “toggle” an passing the value “id”. Next we are using the Document Object Model to get the current state of the display attribute. Then if “display: block;” for the #hidden div we will change it to be “display: none;” else we will change the display to none.

Add the code to your page in the head just below the style.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hidden Div</title>

<style type="text/css">
<!--
#main{
	width:500px;
	height: 20px;
	background: lightblue;
}
#hidden {
	width:300px;
	height:20px;
	background: lightgrey;
	display: none;
}
-->
</style>

<script language="JavaScript">
	function toggle(id) {
		var state = document.getElementById(id).style.display;
			if (state == 'block') {
				document.getElementById(id).style.display = 'none';
			} else {
				document.getElementById(id).style.display = 'block';
			}
		}
</script> 

</head>

<body>
<div id="main">This is not hidden.</div>

<div id="hidden">This is hidden.</div>

</body>
</html>

Now all we have to do is create a link that will call the toggle function and pass the ID of the Div we want to toggle. We will create a link that goes no where and add the onclick property below.

<a href="#" onclick="toggle('hidden');">Toggle Div</a>

Add the link to your visible div.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hidden Div</title>

<style type="text/css">
<!--
#main{
	width:500px;
	height: 20px;
	background: lightblue;
}
#hidden {
	width:300px;
	height:20px;
	background: lightgrey;
	display: none;
}
-->
</style>

<script language="JavaScript">
	function toggle(id) {
		var state = document.getElementById(id).style.display;
			if (state == 'block') {
				document.getElementById(id).style.display = 'none';
			} else {
				document.getElementById(id).style.display = 'block';
			}
		}
</script>

</head>

<body>
<div id="main">
	This is not hidden.
	<a href="#" onclick="toggle('hidden');">Toggle Div</a>
</div>

<div id="hidden">This is hidden.</div>

</body>
</html>

Save the file and test. You should not see the Div when the page loads. When you click the toggle link the hidden box will appear.

Here is an example of something you could use this for. I have a site for deals on clothing. Now if we wanted to add some quality SEO content to help the site rank we could put it in a hidden div so that it doesn’t distract the customer.

When the div is clicked it could expand with SEO rich content and links.

I know the tutorial was pretty basic, but I hope that the example above gives you an idea of how this can be used. I may also expand this into another tutorial and show you how to create a nice menu using the technique.

Popularity: 41% [?]

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit

Related Posts

Tutorial Grad - Recent Tutorials

13 Responses to 'Hide and Show a Div Using Javascript'

Subscribe to comments with RSS or TrackBack to 'Hide and Show a Div Using Javascript'.

  1. Andrew Johnson said,

    on August 28th, 2008 at 2:43 am

    I really like this wish I could figure out how to get this to work with an xml repeat region that I am trying to use then it would be perfect. I can’t seem to find any info on how to use something like this with an xml dataset that I am using.


  2. on September 9th, 2008 at 12:15 pm

    [...] layout and create a 2 Divs. One will be shown and one will be hidden. Here is the starting code. view plaincopy to [...]

  3. sam said,

    on September 15th, 2008 at 1:09 pm

    Hi, will the text within the divs get cached as text by google?

  4. John Ward said,

    on September 15th, 2008 at 3:05 pm

    I believe that google will index the text. It should. The text is still there in the code it is just hidden from the users view, the spider should still see it.

  5. Диплом said,

    on October 28th, 2008 at 8:47 pm

    Инфа что надо!

  6. jade king said,

    on November 27th, 2008 at 1:02 pm

    Hi Im a student who seriously needs help. I got this to work its good.
    Do you no how or any tutorials for making menu’s like this?
    Im trying to make 5 visible toggle divs, one above the other on the left and 5 hidden divs layed over the top of each other in the middle of the page. I want each toggle div to make one of the hidden divs visible while making the other 4 hidden. I basically want a menu that only changes the content of this one spot on the page so that its not the whole page reloading.
    Can you help?

  7. INDINAHOT said,

    on March 20th, 2009 at 5:35 pm

    Was ist das?


  8. on April 8th, 2009 at 8:53 am

    Thank you so much! This tutorial was amazing, and works brilliantly.

    Thanks a million. My gallery works so well now (combined with http://www.dynamicdrive.com/dynamicindex4/thumbnail2.htm).

    Wow. Thanks.

  9. Bong Mendoza said,

    on April 8th, 2009 at 1:50 pm

    tweaked a few codes in the href to get this running with multiple hidden divs.. thanks.

  10. Leib und Seele said,

    on April 9th, 2009 at 12:38 am

    Very clear exposition. Very useful. Thanks!

  11. Todd Davidson said,

    on April 13th, 2009 at 12:00 pm

    Thanks a lot! I used this in the gallery page for my website, and it works perfectly. You’re credited, obviously. Great tutorial, great code.

  12. Buy PSP Go said,

    on August 10th, 2009 at 6:48 am

    This is a good tutorial but be aware that Google doesn’t take too kindly to people who overly stuff keywords.


  13. on October 23rd, 2009 at 12:57 pm

    I just got a blackberry pearl. I wanted to get access to my bank account information on line but when i try log in in, it says: Your browser is not capable of viewing this site because it does not support JavaScript or JavaScript may be disabled.

Leave a Comment