Categories
JavaScript Answers

How to toggle display: none style with JavaScript?

Spread the love

Sometimes, we want to toggle display: none style with JavaScript.

In this article, we’ll look at how to toggle display: none style with JavaScript.

How to toggle display: none style with JavaScript?

To toggle display: none style with JavaScript, we set the style.display property.

For instance, we write

const yourUl = document.getElementById("yourUlId");
yourUl.style.display = yourUl.style.display === "none" ? "" : "none";

to select the element to modify with getElementById.

Then we set its style.display property to '' is it’s currently 'none'.

Otherwise, we set it to 'none'.

Conclusion

To toggle display: none style with JavaScript, we set the style.display property.

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 *