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

Javascript examples for CSS Style Property:visibility

Description

Toggle between hiding and showing an element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {//  ww w.  j  a  v  a2  s.  c om
    width: 500px;
    height: 500px;
    background-color: lightblue;
}
</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.visibility === 'hidden') {
        x.style.visibility = 'visible';
    } else {
        x.style.visibility = 'hidden';
    }
}
</script>

</body>
</html>

Related Tutorials