Style display (style.display) property is used to hide and show the content of HTML.
Example:
document.getElementById("#div").style.display = "none";
Example:
document.getElementById("#div").style.display = "block";
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>
<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;
}
document.getElementById("#div").style.display = "none";
document.getElementById("#div").style.display = "block";
<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>
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