Categories
JavaScript Answers

How to make a div visible and invisible with JavaScript?

Spread the love

Sometimes, we want to make a div visible and invisible with JavaScript.

In this article, we’ll look at how to make a div visible and invisible with JavaScript.

How to make a div visible and invisible with JavaScript?

To make a div visible and invisible with JavaScript, we set the display or visibility properties.

For instance, we write

elem.style.display = 'block';
elem.style.display = 'inline';

elem.style.visibility = 'visible';

to set the display property to 'block' or 'inline' to make the elem element display as a block or inline element.

Or we set visbility to 'visible' to show the element.

To hide the element, we write

elem.style.display = 'none'; 

elem.style.visibility = 'hidden';

to hide the elem element by setting display to 'none' without keeping the space for the element.

Or we hide the element and keep the space for the element by setting visibility to 'hidden‘.

Conclusion

To make a div visible and invisible with JavaScript, we set the display or visibility properties.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *