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.

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>

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.

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.