Javascript Show Hide Div Using Visibility
You can show or hide the div element dynamically using Javascript DOM getElementById method. visibility property of style object can be accessed using getElementById method of document object. visibility property can get or set the value that provides the functionality to toggle the show hide state of HTML <div> tag dynamically. When you use visibility property to hide the div element, it just hides the div layer along with its child HTML elements and inner text but it does not release the space block captured by it on the HTML document.
JavaScript DOM Examples:
You can see the live samples and examples of JavaScript DOM from the following links:
More on visibility property to show or hide the Div
Basically visibility property belongs to the CSS styles that help to hide or show the HTML elements. Javascript DOM style object can access this CSS property that can modify its value dynamically.
visibility: visibility property accepts two types of values hidden or visible. hidden value for visibility property hides the target HTML element where as visible value for the property shows the target HTML element.
You can set the hidden or visible value dynamically using Javascript to show or hide the div element’s visibility.
Javascript Syntax to Show or Hide Div using visibility
document.ElementById("id").style.visibility = "visible";
document.ElementById("id").style.visibility = "hidden";
Javascript Code to Show or Hide Div
<html>
<head>
<title>Javascript Show Hide Div Visibility</title>
<style type="text/css">
.divStyle {
width: 200px;
height: 100px;
margin: 0px auto;
}
</style>
<script language="javascript" type="text/javascript">
<![CDATA[
function showHideDiv() {
var divstyle = new String();
divstyle = document.getElementById("div1").style.visibility;
if (divstyle.toLowerCase() == "visible" || divstyle == "") {
document.getElementById("div1").style.visibility = "hidden";
}
else {
document.getElementById("div1").style.visibility = "visible";
}
}
]]>
</script>
</head>
<body>
<div id="div1" class="divStyle">
Show Hide Div visibility using Javascript DOM.
</div>
<center>
<input type="button"
value="show hide div"
onclick="showHideDiv()" />
</center>
</body>
</html>
Output:
You can see the output of above discussed codes from the following link:
Continue to next tutorial: Javascript Div Collapse Style Using Display None to learn how to release the white space occupied by the Div element by collapsing it using JavaScript DOM style properties.
