0

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;
}
toytu
  • 21
  • 4

3 Answers3

2

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.

freginold
  • 3,946
  • 3
  • 13
  • 28
  • Not really a fan of the semantically meaningless collapseChar, but it's a good workaround. – Snowmonkey Apr 07 '17 at 19:36
  • @Snowmonkey Me neither, but I couldn't think of a better name off the top of my head. – freginold Apr 07 '17 at 19:37
  • Nice. How do I do it if I have another with the same div? – toytu Apr 07 '17 at 19:45
  • @jcdumdumaya Do you mean another header, or another list? If you're using multiple collapsible lists, you'd probably want to create a function and pass the list reference in as a parameter. – freginold Apr 07 '17 at 19:48
  • @freginold I have 2 sets of that Reveal Card. How do i make both work? I tried doing it myself but it won't work – toytu Apr 07 '17 at 20:02
  • @jcdumdumaya You just have to target them individually. As an example, assign the second header to a variable like this: `var header2 = document.getElementsByClassName('RevealCard-header')[1];`. Do the same thing for the list -- call it `list2` (or something else) and grab the 2nd item in the list class (`RevealCard-list`) which would actually be `[1]` since it starts counting from 0. – freginold Apr 07 '17 at 20:07
  • @jcdumdumaya That looks more confusing than it sounded in my head. If you can't get it to work over the weekend, shoot me a comment on here and I'll add a second list to the example, unless someone else can do it sooner. – freginold Apr 07 '17 at 20:08
  • 1
    @jcdumdumaya, Take a look at mine, below. It was a matter of how I accessed the DOM structure of each RevealCard, but the changes were trivial. – Snowmonkey Apr 07 '17 at 20:11
  • It worked. Thank you so much man. I'm kinda new to javascript so I am not used to using eventlistener – toytu Apr 07 '17 at 20:12
1

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>
Tony
  • 2,890
  • 1
  • 24
  • 35
1

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.

Snowmonkey
  • 3,716
  • 1
  • 16
  • 16
  • I like how you used `::after`. – freginold Apr 07 '17 at 19:38
  • 1
    Honestly, wasn't sure I could simply change the ::after's content, so it was a quick hack to create a second class. Can the pseudo-element content be edited via javascript? – Snowmonkey Apr 07 '17 at 19:39
  • Seems the consensus is no, because they are not actually part of the DOM, but there are some workarounds here: [http://stackoverflow.com/questions/10061414/changing-width-property-of-a-before-css-selector-using-jquery](http://stackoverflow.com/questions/10061414/changing-width-property-of-a-before-css-selector-using-jquery) – freginold Apr 07 '17 at 19:47
  • 1
    Yeah, I saw that. Hacky. I think my solution is pretty simple, I guess I'll run with it. He. Thanks, though, @freginold. – Snowmonkey Apr 07 '17 at 19:48