Javascript Div Collapse Style Using Display None
CSS display property can be accessed using Javascript DOM style object that can be utilized to perform the collapse and expand behavior of HTML Div <div> tag. display property can get or set the value that provides the functionality to display none or show the div <div> html element. When you use the display none to hide the div element, it hides the inner text as well as div tag and releases the space blocked by the div whereas visibility property does not release the blocked space. This behavior moves the lower content below the div element to the upward direction when it releases the space while collapsing.
JavaScript DOM Examples:
You can see the live samples and examples of JavaScript DOM from the following links:
More on display property to collapse the div
Basically display property belongs to the CSS styles that helps to expand or collapse the <div> element on the HTML document. Javascript DOM getElementById consists of style object that can access the display property of CSS and allows to modify the value of this property.
display:. display property accepts four types of values among them only two types of values provides the functionality to hide or display the <div> element. none and block are the values that can be used to apply div collapse style
You can set none or block values dynamically using Javascript to expand or collapse the div element.
Javascript Syntax for Div Collapse Style Using Display none
document.getElementById("id").style.display = "none";
document.getElementById("id").style.display = "block";
Javascript Code for Div Collapse Style Using Display none
<html>
<head>
<title>Javascript Div Collapse Style Using Display None</title>
<style type="text/css">
.divStyle {
width: 200px;
height: 100px;
margin: 0px auto;
}
</style>
<script language="javascript" type="text/javascript">
<![CDATA[
function displayDiv() {
var divstyle = new String();
divstyle = document.getElementById("div1").style.display;
if (divstyle.toLowerCase() == "block" || divstyle == "") {
document.getElementById("div1").style.display = "none";
}
else {
document.getElementById("div1").style.display = "block";
}
}
]]>
</script>
</head>
<body>
<div id="div1" class="divStyle">
Set CSS display property to none or block using Javascript DOM dynamically.
</div>
<center>
<input type="button"
value="display Div"
onclick="displayDiv()" />
</center>
</body>
</html>
Output:
You can see the output of above discussed codes from the following link:
Continue to next tutorial: Javascript Change Image onmouseover to learn how to rollover image onmouseover event using JavaScript.
