{"id":1612,"date":"2008-07-27T11:01:47","date_gmt":"2008-07-27T15:01:47","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1612"},"modified":"2008-10-26T14:45:38","modified_gmt":"2008-10-26T18:45:38","slug":"hide-and-show-a-div-using-javascript","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/hide-and-show-a-div-using-javascript","title":{"rendered":"Hide and Show a Div Using Javascript"},"content":{"rendered":"
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\u2019s 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.<\/p>\n
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.<\/p>\n
\r\n<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">\r\n<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\r\n<head>\r\n<title>Hidden Div<\/title>\r\n<\/head>\r\n\r\n<body>\r\n<div id="main">This is not hidden.<\/div>\r\n\r\n<div id="hidden">This is hidden.<\/div>\r\n\r\n\r\n<\/body>\r\n<\/html>\r\n\r\nOk now I will add some basic style to these boxes.\r\n<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">\r\n<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\r\n<head>\r\n<title>Hidden Div<\/title>\r\n\r\n<style type="text\/css">\r\n<!--\r\n#main{\r\n\twidth:500px;\r\n\theight: 20px;\r\n\tbackground: lightblue;\r\n}\r\n#hidden {\r\n\twidth:300px;\r\n\theight:20px;\r\n\tbackground: lightgrey;\r\n}\r\n-->\r\n<\/style>\r\n\r\n\r\n<\/head>\r\n\r\n<body>\r\n<div id="main">This is not hidden.<\/div>\r\n\r\n<div id="hidden">This is hidden.<\/div>\r\n\r\n\r\n<\/body>\r\n<\/html>\r\n<\/pre>\nThe page should look like this so far.
\n<\/a><\/p>\nNow 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.<\/p>\n
\r\n<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">\r\n<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\r\n<head>\r\n<title>Hidden Div<\/title>\r\n\r\n<style type="text\/css">\r\n<!--\r\n#main{\r\n\twidth:500px;\r\n\theight: 20px;\r\n\tbackground: lightblue;\r\n}\r\n#hidden {\r\n\twidth:300px;\r\n\theight:20px;\r\n\tbackground: lightgrey;\r\n\tdisplay: none;\r\n}\r\n-->\r\n<\/style>\r\n\r\n\r\n<\/head>\r\n\r\n<body>\r\n<div id="main">This is not hidden.<\/div>\r\n\r\n<div id="hidden">This is hidden.<\/div>\r\n\r\n\r\n<\/body>\r\n<\/html>\r\n<\/pre>\nNow when you preview the page you will not see the Div.
\n<\/a><\/p>\nIn 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.<\/p>\n
\r\n<script language="JavaScript">\r\n\tfunction toggle(id) {\r\n\t\tvar state = document.getElementById(id).style.display;\r\n\t\t\tif (state == 'block') {\r\n\t\t\t\tdocument.getElementById(id).style.display = 'none';\r\n\t\t\t} else {\r\n\t\t\t\tdocument.getElementById(id).style.display = 'block';\r\n\t\t\t}\r\n\t\t} \r\n<\/script>\r\n<\/pre>\nThe script above is creating the function \u201ctoggle\u201d an passing the value \u201cid\u201d. Next we are using the Document Object Model to get the current state of the display attribute. Then if \u201cdisplay: block;\u201d for the #hidden div we will change it to be \u201cdisplay: none;\u201d else we will change the display to none. <\/p>\n
Add the code to your page in the head just below the style.<\/p>\n
\r\n<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">\r\n<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\r\n<head>\r\n<title>Hidden Div<\/title>\r\n\r\n<style type="text\/css">\r\n<!--\r\n#main{\r\n\twidth:500px;\r\n\theight: 20px;\r\n\tbackground: lightblue;\r\n}\r\n#hidden {\r\n\twidth:300px;\r\n\theight:20px;\r\n\tbackground: lightgrey;\r\n\tdisplay: none;\r\n}\r\n-->\r\n<\/style>\r\n\r\n<script language="JavaScript">\r\n\tfunction toggle(id) {\r\n\t\tvar state = document.getElementById(id).style.display;\r\n\t\t\tif (state == 'block') {\r\n\t\t\t\tdocument.getElementById(id).style.display = 'none';\r\n\t\t\t} else {\r\n\t\t\t\tdocument.getElementById(id).style.display = 'block';\r\n\t\t\t}\r\n\t\t} \r\n<\/script> \r\n\r\n\r\n<\/head>\r\n\r\n<body>\r\n<div id="main">This is not hidden.<\/div>\r\n\r\n<div id="hidden">This is hidden.<\/div>\r\n\r\n\r\n<\/body>\r\n<\/html>\r\n<\/pre>\nNow 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.<\/p>\n
\r\n<a href="#" onclick="toggle('hidden');">Toggle Div<\/a>\r\n<\/pre>\nAdd the link to your visible div.<\/p>\n
\r\n<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">\r\n<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\r\n<head>\r\n<title>Hidden Div<\/title>\r\n\r\n<style type="text\/css">\r\n<!--\r\n#main{\r\n\twidth:500px;\r\n\theight: 20px;\r\n\tbackground: lightblue;\r\n}\r\n#hidden {\r\n\twidth:300px;\r\n\theight:20px;\r\n\tbackground: lightgrey;\r\n\tdisplay: none;\r\n}\r\n-->\r\n<\/style>\r\n\r\n<script language="JavaScript">\r\n\tfunction toggle(id) {\r\n\t\tvar state = document.getElementById(id).style.display;\r\n\t\t\tif (state == 'block') {\r\n\t\t\t\tdocument.getElementById(id).style.display = 'none';\r\n\t\t\t} else {\r\n\t\t\t\tdocument.getElementById(id).style.display = 'block';\r\n\t\t\t}\r\n\t\t}\r\n<\/script>\r\n\r\n\r\n<\/head>\r\n\r\n<body>\r\n<div id="main">\r\n\tThis is not hidden.\r\n\t<a href="#" onclick="toggle('hidden');">Toggle Div<\/a>\r\n<\/div>\r\n\r\n<div id="hidden">This is hidden.<\/div>\r\n\r\n\r\n<\/body>\r\n<\/html>\r\n<\/pre>\nSave 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.
\n<\/a><\/p>\nHere 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\u2019t distract the customer.
\n<\/a><\/p>\n