hide and show div tag in JS

** Setting the display property of an element only changes how the element is displayed, NOT what kind of element it is. So, an inline element with display: block; is not allowed to have other block elements inside it.


The display property is used to specify how an element is shown on a web page.

Every HTML element has a default display value, depending on what type of element it is. The default display value for most elements is block or inline.

The display property is used to change the default display behavior of HTML elements.


display: none; is commonly used with JavaScript to hide and show elements without deleting and recreating them.


visibility:hidden; also hides an element.

However, the element will still take up the same space as before. The element will be hidden, but still affect the layout:

Example

h1.hidden {
  visibility: hidden;

}     



<!DOCTYPE html>

    <html>

    <body>

    

    <button onclick="toggleDiv()">Toggle Div</button>

    

    <div id="myDiv" style="display: none;">This is my div!</div>

    

    <div id="myDiv1" style="display: none;">This is my divjjj!</div>

    

    <script>

    function toggleDiv() {

    y=5;

      var x = document.getElementById("myDiv");

      var x1 = document.getElementById("myDiv1");

      if (y<1) {

        x.style.display = "block";

      } else {

        x1.style.display = "block";

      }

    }

    </script>

    

    </body>

    </html>

Comments

Popular posts from this blog

image work in springboot

Springboot important annotation