07.27.2008

Hide and Show a Div Using Javascript

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.

Related Posts

Comments

  1. Andrew Johnson on August 28, 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.

    [Reply]

  2. Hide and Show a Div Using Javascript - Blue Box Sols on September 9, 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 on September 15, 2008 at 1:09 pm

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

    [Reply]

  4. John Ward on September 15, 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.

    [Reply]

  5. Диплом on October 28, 2008 at 8:47 pm

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

    [Reply]

  6. jade king on November 27, 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?

    [Reply]

  7. INDINAHOT on March 20, 2009 at 5:35 pm

    Was ist das?

    [Reply]

  8. Todd Davidson on April 8, 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.

    [Reply]

  9. Bong Mendoza on April 8, 2009 at 1:50 pm

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

    [Reply]

  10. Leib und Seele on April 9, 2009 at 12:38 am

    Very clear exposition. Very useful. Thanks!

    [Reply]

  11. Todd Davidson on April 13, 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.

    [Reply]

  12. Buy PSP Go on August 10, 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.

    [Reply]

  13. resort for couples on October 23, 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.

    [Reply]

  14. Sanjib on December 17, 2009 at 12:21 am

    Client Account

    Company Name

    Address

    Phone Number

    Back Panel

    Rate
    Quantity
    Route Details

    Bus Branding

    Rate
    Quantity
    Route Details

    3D Bus Branding

    Rate
    Quantity
    Route Details

    function showHiddenDiv(id)
    {
    document.getElementById(id).style.display == “none” ? (document.getElementById(id).style.display=”block”):(document.getElementById(id).style.display=”none”)
    }

    [Reply]

  15. Rob Busby on May 28, 2010 at 10:16 am

    Although this script is not what I was looking for, as it is fairly simple, I wanted to at least take a few moments to thank you for posting the information for newer users. Also, to point out one flaw…please forgive me. If you are using this script and would like for your script to stay w3c compliant, you may want to add the color in your css script. For example: instead using “lightblue” or “lightgrey”, you may want to use the code specific equivalent: #9cf or #ccc (respectively). You could also use “background-color” instead of “background” in order to be completely specific.

    Again, thanks for this information. I know it will help out future viewers of your site.

    [Reply]

  16. Rajesh on June 15, 2010 at 5:27 am

    Thanx a lot for this script!!

    [Reply]

    Reena s Reply:

    Thank you so much!!! for posting this tutorial…very simple and when applied works flawless

    [Reply]

  17. Mei on August 7, 2010 at 1:59 pm

    Thanks for posting this code online. Awesome tutorial!

    [Reply]