# \[JS\] Hide/Show divs

**URL:** <https://forum.kirupa.com/t/js-hide-show-divs/243567>\
**Category:** web dev\
**Created:** [November 8, 2007, 1:58pm UTC](https://forum.kirupa.com/t/js-hide-show-divs/243567 "2007-11-08T13:58:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bardic](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bardic/32/3708_2.png) [@bardic](https://forum.kirupa.com/u/bardic)\
**Post date:** [November 8, 2007, 1:58pm UTC](https://forum.kirupa.com/t/js-hide-show-divs/243567/1 "2007-11-08T13:58:27Z")

</div>

I can hide and show a div fine. The problem arises when I want it to hide other div that I opened before it.

```auto

var openDiv=""; 
function toggleDiv(divid){
    if(document.getElementById(divid).style.display == 'none'){ 
      if(openDiv != ""){
          document.getElementById(openDiv).style.display = 'none';
       }
       document.getElementById(divid).style.display = 'block';
      var openDiv = divid;
    }else{
      document.getElementById(divid).style.display = 'none';
    }
  }

```

There’s the code I am trying to get it to work but it just seems to break. Any help or tips would be much appreciated.
