JavaScript Style Display property is used to hide or show elements in HTML

Style display (style.display) property is used to hide and show the content of HTML.
If you want to hide an element, then set the style.display property to 'none'.
Example:
document.getElementById("#div").style.display = "none";
If you want to show an element, then set the style.display property to 'block'.
Example:
document.getElementById("#div").style.display = "block";
Now, Going to show how style display works with 3 steps.
Step 1: First creating some div and assign them an id or class.
<div class="circle" id="circle"></div> 
<div class="rounded" id="rounded"></div> 
<div class="square" id="square"></div>
Step 2: Add style for those div.
<style type="text/css"> 
#circle { 
background-color: green; 
width: 200px; 
height: 200px; 
border-radius: 25%; 
float: left; 
margin-right: 40px; 
.rounded 
background-color: black; 
width: 200px; 
height: 200px; 
border-radius: 25%; 
float: left; 
margin-right: 40px; 
.square { 
background-color: red; 
width: 200px; 
height: 200px; 
border-radius: 0%; 
float: left; 
margin-right: 40px; 
}
Step 3: Now use style.display property for hide or show the HTML Element.
document.getElementById("#div").style.display = "none";
or
document.getElementById("#div").style.display = "block";
Below is the implementation of full code with style.display property.
<html> 
 <head> 
 <title>style.display property</title> 
 <style type="text/css"> 
  #circle { 
   background-color: green; 
   width: 200px; 
   height: 200px; 
   border-radius: 25%; 
   float: left; 
   margin-right: 40px; 
   } 
  .rounded { 
   background-color: black; 
   width: 200px; 
   height: 200px; 
   border-radius: 25%; 
   float: left; 
   margin-right: 40px; 
   } 
  .square { 
   background-color: red; 
   width: 200px; 
   height: 200px; 
   border-radius: 0%; 
   float: left; 
   margin-right: 40px; 
   } 
 </style> 
 </head> 
 <body> 
  <div class="circle" id="circle"></div> 
  <div class="rounded" id="rounded"></div> 
  <div class="square" id="square"></div> 
 </body> 
</html> 
<script type="text/javascript"> 
document.getElementById("circle").onclick = function() {           document.getElementById("circle").style.display = "none"; } document.getElementById("rounded").onclick = function() {     document.getElementById("rounded").style.display = "none"; } document.getElementById("square").onclick = function() {   document.getElementById("square").style.display = "none"; } 
</script>
Watching this Video, you can learn Hide/show elements in HTML using display property.


Project Source Code:
<!DOCTYPE html> 
<html> 
 <body> 
  <h2>What Can JavaScript Do?</h2> 
  <p id="demo">JavaScript can hide HTML elements.</p> 
  <button type="button" onclick="hideP();">Click to Hide the Paragraph</button> 
 </body> 
</html> 
<script type="text/javascript"> 
 function hideP(){ 
  document.getElementById("demo").style.display = 'none'; } 
</script>


EmoticonEmoticon