/

How to Hide a DOM Element Using Plain JavaScript

How to Hide a DOM Element Using Plain JavaScript

In this blog post, we will explore how to hide and show DOM elements using vanilla JavaScript. Manipulating the CSS properties of an element can achieve this effect.

To hide a DOM element, you can utilize the style property of the element. The style property provides access to modify the CSS styling properties.

To hide an element, you can set the display property to 'none', similar to the CSS declaration display: none;:

1
element.style.display = 'none';

This line of code will hide the element.

To display the hidden element again, you need to set its display property back to 'block' (for block elements) or 'inline' (for inline elements):

1
element.style.display = 'block';

By manipulating the style property with the appropriate values, you can easily hide or show DOM elements using plain JavaScript.

Tags: JavaScript, DOM manipulation, CSS styling