Hide or show Elements/div in HTML Using JavaScript and Css

Sh Raj - Jan 22 '22 - - Dev Community

Steps :-

  • For This Firstly create a .hidden class where the css display property is set to "none".
<style>
  .hidden{
    display:none;
  }
</style>
Enter fullscreen mode Exit fullscreen mode

HTML

  • Then Using JavaScript we will toggle (add / remove) the hidden class from the element. So that results hiding and showing of the div/any other element.
  • Creating a JavaScript function to hide/show elements.
<script>
  function hideunhide(a){
    document.querySelector(a).classList.toggle('hidden');
  }
</script>
Enter fullscreen mode Exit fullscreen mode

HTML

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .