Toggle between showing and hiding an element: - Javascript CSS Style Property

Javascript examples for CSS Style Property:display

Description

Toggle between showing and hiding an element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {//  w  ww.  ja  v a  2s  . co m
    width: 200px;
    height: 200px;
    background-color: red;
}
</style>
</head>
<body>

<button onclick="myFunction()">Test</button>

<div id="myDIV">
This is my DIV element.
</div>

<script>
function myFunction() {
    var x = document.getElementById('myDIV');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}
</script>

</body>
</html>

Related Tutorials