JavaScript Hide Div
In this section, we are going to hide div element on clicking the button using the JavaScript.
In the given example, we have created a div element using <div> tag. The method document.getElementById('div') grabs the text of the div element and refer to property 'visibility' with style object. The 'visibility' property makes the element visible or invisible. Now, if you apply the visibility property with the 'hidden' value to the div element, the text of the div element will disappear and if you use the 'visible' value, the text will be visible again. When you load the page you will get two buttons and text will be visible. But on clicking the button 'Hide', the function hideDiv() is called that makes the text invisible.
Here is the code:
| <html> <h2>Hide Div in JavaScript</h2> <script language=javascript type='text/javascript'> function hideDiv() { if (document.getElementById) { document.getElementById('div').style.visibility = 'hidden'; } } function showDiv() { if (document.getElementById) { document.getElementById('div').style.visibility = 'visible'; } } </script> <body onload="javascript:showDiv()"> <button onclick="javascript:hideDiv()">Hide</button> <button onclick="javascript:showDiv()">Show</button> <div id="div">Hello World</div> </body> </html> |
Output will be displayed as:
On clicking the button 'Hide', the text will get disappear:

Tutorials
- Clear cookie example
- JavaScript getElementById innerHTML
- JavaScript getElementById Style
- Javascript Examples
- JavaScript add row dynamically to table
- New Page 1
- JavaScript Change link
- JavaScript Checkbox getElementById
- javascript clear textarea
- JavaScript Clock Timer
- JavaScript Cookies
- JavaScript Date Difference
- JavaScript duplicate string
- JavaScript Email Validation
- javascript focus input
- JavaScript get excel file data
- JavaScript getAttribute Href
- JavaScript getAttribute Style
- JavaScript getElementById div
- JavaScript getElementById Iframe
- JavaScript getElementById select
- JavaScript Hide Button
- JavaScript Hide Div
- JavaScript hide image
- JavaScript Hide Table Column
- JavaScript Hide Table Rows
- JavaScript Key Event
- JavaScript link
- JavaScript method location
- JavaScript move div
- JavaScript move file
- JavaScript move image
- JavaScript Navigate Back
- JavaScript navigate to page
- JavaScript Navigate to URL
- JavaScript indexOf substring
- JavaScript onkeypress event
- JavaScript Open file
- JavaScript Open link in new window
- JavaScript Open Modal Window


