toytu
toytu

Reputation: 13

Javascript show and hide

How do I make this when I click the header it will hide or show the List? and when its visible the header shows - and when its not visible it shows a +. Thanks a lot

 <div class="RevealCard">
  <h3 class="RevealCard-header">
  Top five loves
  </h3>
  <ol class="RevealCard-list">
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
  <li>Fifth item</li>
  </ol>
 </div>

Here is the CSS

RevealCard-header {
  background: #000;
  border-radius: 4px 4px 0 0;
  color: #fff;
  font-size: 1.2em;
  font-weight: normal;
  margin: 0;
  padding: 0.5em;
  position: relative;
}

.RevealCard-header::after {
  border: 1px solid #fff;
  content: "-";
  height: 1.15em;
  line-height: 1em;
  position: absolute;
  right: 0.5em;
  text-align: center;
  width: 1.15em;
}

.RevealCard-list {
  border: 1px solid #000;
  border-top: none;
  margin: 0 0 2em 0;
  padding-bottom: 1em;
  padding-top: 1em;
}

Upvotes: 0

Views: 70

Answers (3)

Snowmonkey
Snowmonkey

Reputation: 3761

Not only do you want to toggle the OL element, you also want to change the pseudo ::after css. Easiest way? Create a second css ::after style, and simply toggle them both. See below.

var myHead = document.getElementsByClassName("RevealCard-header");
for (var i = 0, j = myHead.length; i  < j; i++) {
  myHead[i].addEventListener("click", function() {
    var myTab = this.parentNode.querySelector("ol");

    if (myTab.style.display == "") {
      // Because of the pseudo ::after el, we'll
      //  create a showing/hiding class.
      this.classList.remove("header-showing");
      this.classList.add("header-hiding");
      myTab.style.display = "none"
    } else {
      this.classList.remove("header-hiding");
      this.classList.add("header-showing");
      myTab.style.display = "";
    }
  })
}
.RevealCard {
  width: 45%;
  float: left;
  padding: 5px;
}

.RevealCard-header {
  background: #000;
  border-radius: 4px 4px 0 0;
  color: #fff;
  font-size: 1.2em;
  font-weight: normal;
  margin: 0;
  padding: 0.5em;
  position: relative;
}

.header-showing::after {
  border: 1px solid #fff;
  content: "-";
  height: 1.15em;
  line-height: 1em;
  position: absolute;
  right: 0.5em;
  text-align: center;
  width: 1.15em;
}

.header-hiding::after {
  border: 1px solid #fff;
  content: "+";
  height: 1.15em;
  line-height: 1em;
  position: absolute;
  right: 0.5em;
  text-align: center;
  width: 1.15em;
}

.RevealCard-list {
  border: 1px solid #000;
  border-top: none;
  margin: 0 0 2em 0;
  padding-bottom: 1em;
  padding-top: 1em;
}
<div class="RevealCard">
  <h3 class="RevealCard-header header-showing">
    Top five loves
  </h3>
  <ol class="RevealCard-list">
    <li>First item</li>
    <li>Second item</li>
    <li>Third item</li>
    <li>Fourth item</li>
    <li>Fifth item</li>
  </ol>
</div>

<div class="RevealCard">
  <h3 class="RevealCard-header header-showing">
    Another five loves
  </h3>
  <ol class="RevealCard-list">
    <li>First item</li>
    <li>Second item</li>
    <li>Third item</li>
    <li>Fourth item</li>
    <li>Fifth item</li>
  </ol>
</div>

Updated to support multiple sets. You do this by attaching the listener to the head, then working within its own parent's DOM structure.

Upvotes: 1

Tony
Tony

Reputation: 3058

This should do it:

function clickDropdown(){

  var listitems = document.getElementById("RevealCard");
  var header = document.getElementById("RevealCard-header").innerHTML;
  
  if(listitems.style.display == "none"){
    listitems.style.display = 'block';
    
    header = header.replace('+', '-');
  document.getElementById("RevealCard-header").innerHTML = header;
  }else{
    listitems.style.display = 'none';
    header = header.replace('-','+');
    document.getElementById("RevealCard-header").innerHTML = header;
  }

}
RevealCard-header {
  background: #000;
  border-radius: 4px 4px 0 0;
  color: #fff;
  font-size: 1.2em;
  font-weight: normal;
  margin: 0;
  padding: 0.5em;
  position: relative;
}

.RevealCard-header::after {
  border: 1px solid #fff;
  content: "-";
  height: 1.15em;
  line-height: 1em;
  position: absolute;
  right: 0.5em;
  text-align: center;
  width: 1.15em;
}

.RevealCard-list {
  border: 1px solid #000;
  border-top: none;
  margin: 0 0 2em 0;
  padding-bottom: 1em;
  padding-top: 1em;
}
<div class="RevealCard">
<a onClick="clickDropdown()">  <h3 id="RevealCard-header">
  Top five loves +
  </h3></a>
  <ol style="display: none;" id="RevealCard">
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
  <li>Fifth item</li>
  </ol>
 </div>

Upvotes: 1

freginold
freginold

Reputation: 3956

Here's a working solution:

var header = document.getElementsByClassName('RevealCard-header')[0];
var list = document.getElementsByClassName('RevealCard-list')[0];
var collapseChar = document.getElementById('collapseChar');

header.addEventListener('click', function(){
	// toggle list on click
  if (list.style.display.toLowerCase() != "none") {
  	// hide list
    list.style.display = "none";
    collapseChar.innerHTML = "&plus;";
  }
  else {
  	// show list
    list.style.display = "block";
    collapseChar.innerHTML = "&minus;";    
  }
})
RevealCard-header {
  background: #000;
  border-radius: 4px 4px 0 0;
  color: #fff;
  font-size: 1.2em;
  font-weight: normal;
  margin: 0;
  padding: 0.5em;
  position: relative;
}

.RevealCard-header::after {
  border: 1px solid #fff;
  content: "-";
  height: 1.15em;
  line-height: 1em;
  position: absolute;
  right: 0.5em;
  text-align: center;
  width: 1.15em;
}

.RevealCard-list {
  border: 1px solid #000;
  border-top: none;
  margin: 0 0 2em 0;
  padding-bottom: 1em;
  padding-top: 1em;
}

#collapseChar {
  margin-right: 3px;
}
<div class="RevealCard">
  <h3 class="RevealCard-header">
  <span id="collapseChar">&minus;</span>Top five loves
  </h3>
  <ol class="RevealCard-list">
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
  <li>Fifth item</li>
  </ol>
 </div>

You just have to check to see if the list is visible. If it's not, show it; if it is, hide it.

It's also here as a fiddle, if you want to play around with it.

Upvotes: 2

Related Questions