Sometimes, we want to show or hide ‘div’ using JavaScript.
In this article, we’ll look at how to show or hide ‘div’ using JavaScript.
How to show or hide ‘div’ using JavaScript?
To show or hide ‘div’ using JavaScript, we set the style.display
property.
For instance, we write
<div id="attid" style="display: none">Show/Hide this text</div>
to add a div.
Then we write
if (show) {
document.getElementById("attid").style.display = "inline";
} else {
document.getElementById("attid").style.display = "none";
}
to select the div with getElementById
.
Then set the style.display
property to 'inline'
when show
is true
.
Otherwise, we set the style.display
property to 'none'
.
Conclusion
To show or hide ‘div’ using JavaScript, we set the style.display
property.