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.

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>

Continue to next tutorial: Javascript Change Image onmouseover to learn how to rollover image onmouseover event using JavaScript.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.