Javascript DOM CSS Style visibility Property toggle show and hide

Introduction

Toggle between hiding and showing an element:

Click the button to toggle between hiding and showing the DIV element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {/*from  www  . j  ava  2s  .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>

<div>
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>



PreviousNext

Related