Multiple Modal

Cannot close modal windows

by Haley Smith

HTML

<div class="contentSection">
  <a class="popupBtn" data-modal="modal-window-one">Open Me!</a>
</div>

<div id="modal-window-one" class="popupModal modal">
  <span class="close">&times;</span>
  <div class="modalContent">
    <p>
      Here's some content!
    </p>
  </div>
</div>

<div class="contentSection">
  <a class="popupBtn" data-modal="modal-window-two">...Or Open Me!</a>
</div>

<div id="modal-window-two" class="popupModal modal">
  <span class="close">&times;</span>
  <div class="modalContent">
    <p>
      Here's some different content!
    </p>
  </div>
</div>

CSS

.popupBtn{
  cursor: pointer;
}

.modal {
    display: none;
    position: fixed; 
    z-index: 1; 
    left: 0;
    top: 0;
    width: 100%; 
    height: 100%; 
    background-color: rgb(0,0,0); 
    background-color: rgba(0,0,0,0.4); 
    cursor: pointer;
}

.modalContent {
    background-color: #fefefe;
    margin: 15% auto;
    padding: 20px;
    border-top: 8px solid #81c8e8;
    border-radius: 4px;
    width: 50%;
    box-shadow: 0px 2px 8px #222;
    cursor: default;
    font-size: .9em;
}

.close {
    color: #ccc;
    float: right;
    font-size: 25pt;
    font-weight: bold;
    transition: ease-in-out .5s;
    padding: 10px 30px;
    background-color: #2b3d52;
}

.close:hover,
.close:focus {
    color: #fff;
    text-decoration: none;
    cursor: pointer;
}

JavaScript

var modal = document.getElementsByClassName('popupModal');
  var btn = document.getElementsByClassName("popupBtn");
  var span = document.getElementsByClassName("close")[0];

  for (var i = 0; i < btn.length; i++) {
    var thisBtn = btn[i];
    thisBtn.addEventListener("click", function(){
      var modal = document.getElementById(this.dataset.modal);
      modal.style.display = "block";
    }, false);
  }

  span.onclick = function() {
    modal.style.display = "none";
  }

  window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
  }