How to set an element's display type with JavaScript?


To set an element’s display type, use the display property. If you want to hide the element, then use none.

Example

You can try to run the following code to set an element's display type with JavaScript −

Live Demo

<!DOCTYPE html>
<html>
   <head>
      <style>
         #myID {
            width: 250px;
            height: 200px;
            background-color: green;
         }
      </style>
   </head>
   <body>
      <button onclick="display()">Set display as none</button>
      <div id="myID">
         <h1>Heading Level 1 for Demo</h1>
         This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text.
      </div>
      <script>
         function display() {
            document.getElementById("myID").style.display = "none";
         }
      </script>
   </body>
</html>

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

794 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements