Hide/Show an Element by ID using JavaScript

avatar

Borislav Hadzhiev

Thu Jan 06 20222 min read

Hide/Show an Element by ID using JavaScript #

To hide/show an element by id, use the style.display property to determine if the element is shown or hidden. If its display property is set to none, show the element by setting it to block, otherwise hide the element by setting its display property to none.

Here is the HTML for the examples in this article.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> #container { background-color: lime; width: 150px; height: 150px; } </style> </head> <body> <div id="container">Container content</div> <button id="btn">Hide element</button> <script src="index.js"></script> </body> </html>

And here is the related JavaScript code.

index.js
const el = document.getElementById('container'); const btn = document.getElementById('btn'); btn.addEventListener('click', function handleClick() { if (el.style.display === 'none') { el.style.display = 'block'; btn.textContent = 'Hide element'; } else { el.style.display = 'none'; btn.textContent = 'Show element'; } });

We added a click event listener to a button. Every time the button is clicked, the handleClick function is invoked.

In the function, we check if the element's display property is set to none.

If the element has a display property set to none, then it is hidden, in which case, we set it's display property to block to show the element.

Otherwise, the element is shown, so we set its display property to none to hide it.

We also used the textContent property, to update the button's text when the element is hidden / shown.

We used the display property in the examples, however you might need to use the visibility property depending on your use case.

When an element's display property is set to none, the element is removed from the DOM and has no effect on the layout. The document is rendered as though the element does not exist.

On the other hand, when an element's visibility property is set to hidden, it still takes up space on the page, however the element is invisible (not drawn). It still affects the layout on your page as normal.

Here is an example that uses the visibility property to hide/show an element by its id.

index.js
const el = document.getElementById('container'); const btn = document.getElementById('btn'); btn.addEventListener('click', function handleClick() { if (el.style.visibility === 'hidden') { el.style.visibility = 'visible'; btn.textContent = 'Hide div'; } else { el.style.visibility = 'hidden'; btn.textContent = 'Show div'; } });
If you click on the button, you will see that the element is hidden, however the button doesn't take its place on the page.

Even though the element is not rendered, it still affects the layout on the page as normal.

When we used the display property to hide the element, the button would take its place in the DOM as the div element got completely removed from the document.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee