Jono Simko
Jono Simko

Reputation: 3

Click an anchor to reveal hidden div beneath, then hide all other content divs?

I've created 4 containers which can be clicked to reveal content beneath. I would like to show only one hidden div at once. So, if a user clicks another container, all others are hidden. Current I cannot hide any of the containers once they have been clicked. I really appreciate any help :)

JS

function toggle_visibility(id) {
  var e = document.getElementById(id);
  if(e.style.display == 'none')
     e.style.display = 'block';
  else
     e.style.display = 'none';
}

CSS

html, body { height: 100%; padding: 0 ; margin: 0; }
a { width: 100%; height: 100%; color: #000; display: block; position: absolute; background: #647070; }
.section { width: 49.9%; height: 49.9%; float: left; position: relative; overflow: hidden; }
#div1, #div3 { border-right: 1px solid black; }
#div3, #div4 { border-top: 1px solid black; }

HTML

  <div id="div1" class="section">
    <div id="festival">
      <a href="#" onclick="toggle_visibility('festival');" style="">Festival&trade;</a>
    </div>
     <p>This is the content of Q1</p>
  </div>

  <div id="div2" class="section">
    <div id="register">
      <a href="#" onclick="toggle_visibility('register');" style="">Register</a>
    </div>
    <p>This is the content of Q2</p>
  </div>

  <div id="div3" class="section">
    <div id="connect">
      <a href="#" onclick="toggle_visibility('connect');" style="">Connect</a>
    </div>
    <p>This is the Q3 content.</p>
  </div>

  <div id="div4" class="section">
    <div id="forth">
      <a href="#" onclick="toggle_visibility('forth');" style="">Forth</a>
    </div>
    <p>This is the Q4 content.</p>
  </div> 

Upvotes: 0

Views: 108

Answers (1)

You could add something like this:

var divsToHide = document.querySelectorAll(".section > div")
for (var i = 0; i < divsToHide.length; i++) {
  divsToHide[i].style.display = "block";
}

This will loop through each of the .section and show the direct div of it.

Demo

function toggle_visibility(id) {

  var divsToHide = document.querySelectorAll(".section > div")
  for (var i = 0; i < divsToHide.length; i++) {
    divsToHide[i].style.display = "block";
  }
  var e = document.getElementById(id);
  if (e.style.display == 'none')
    e.style.display = 'block';
  else
    e.style.display = 'none';
}
html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
}

a {
  width: 100%;
  height: 100%;
  color: #000;
  display: block;
  position: absolute;
  background: #647070;
}

.section {
  width: 49.9%;
  height: 49.9%;
  float: left;
  position: relative;
  overflow: hidden;
}

#div1,
#div3 {
  border-right: 1px solid black;
}

#div3,
#div4 {
  border-top: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div1" class="section">
  <div id="festival">
    <a href="#" onclick="toggle_visibility('festival');" style="">Festival&trade;</a>
  </div>
  <p>This is the content of Q1</p>
</div>

<div id="div2" class="section">
  <div id="register">
    <a href="#" onclick="toggle_visibility('register');" style="">Register</a>
  </div>
  <p>This is the content of Q2</p>
</div>

<div id="div3" class="section">
  <div id="connect">
    <a href="#" onclick="toggle_visibility('connect');" style="">Connect</a>
  </div>
  <p>This is the Q3 content.</p>
</div>

<div id="div4" class="section">
  <div id="forth">
    <a href="#" onclick="toggle_visibility('forth');" style="">Forth</a>
  </div>
  <p>This is the Q4 content.</p>
</div>

Upvotes: 2

Related Questions