Categories
JavaScript Answers

How to hide an HTML element by ID with JavaScript?

Spread the love

Sometimes, we want to hide an HTML element by ID with JavaScript.

In this article, we’ll look at how to hide an HTML element by ID with JavaScript.

How to hide an HTML element by ID with JavaScript?

To hide an HTML element by ID with JavaScript, we can set the style.display property to 'none' or the style.visibility property to 'hidden'.

For instance, we write:

<div class="nav">
  <ul>
    <li>
      <a id="nav-ask" href="/questions/ask">
        Ask Question
      </a>
    </li>
  </ul>
</div>

to add some elements.

Then we can hide the a element by writing:

const link = document.getElementById('nav-ask');
link.style.display = 'none';

or

const link = document.getElementById('nav-ask');
link.style.visibility = 'hidden';

Conclusion

To hide an HTML element by ID with JavaScript, we can set the style.display property to 'none' or the style.visibility property to 'hidden'.

By John Au-Yeung

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

One reply on “How to hide an HTML element by ID with JavaScript?”

Leave a Reply

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