-1

I use this Modal for multiple links on one page of my website. What I need is a script / code that closes the modal window when user clicks outside. I found this post, which works, but only for one link, since the script requires id. Is there a way to apply this script for multiple ID's? Or other methods?

Community
  • 1
  • 1
Alin
  • 13
  • 1
  • 2
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Daniel Corzo Dec 22 '16 at 22:13
  • There's no JavaScript here... – Praveen Kumar Purushothaman Dec 22 '16 at 22:17
  • There is js in the mentioned post. – Alin Dec 22 '16 at 22:31
  • @Alin Check my answer now. – Praveen Kumar Purushothaman Dec 22 '16 at 22:37

1 Answers1

1

This is based on the id, as it works out of :target, so what you need to do is, give a common class and different IDs for different modals like this:

window.onload = function () {
  var modals = document.getElementsByClassName("modal");
  var windows = document.getElementsByClassName("window");

  var clearModal = function () {
    location.hash = '';
  };

  for (var i = 0; i < modals.length; i++) {
    modals[i].addEventListener('click', clearModal, false);
  }
  for (var i = 0; i < windows.length; i++) {
    windows[i].addEventListener('click', function () {
      event.stopPropagation();
      event.cancelBubble = true;
    }, false);
  }
  document.body.addEventListener('keydown', function () {
    if (event.keyCode == 27) {
      location.hash = '';
      clearModal();
    }
  }, false);
};
* {
  margin: 0;
  padding: 0;
  list-style: none;
}
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.25);
  z-index: 9999;
  pointer-events: none;
  opacity: 0;
}
.modal .window {
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 300px;
  height: 200px;
  margin: -100px 0 0 -150px;
  text-align: center;
  line-height: 120px;
}
.modal:target {
  opacity: 1;
  pointer-events: auto;
}
<div id="modal-1" class="modal">
  <div class="window">Modal Window 1<br /><a href="#">Close</a></div>
</div>
<div id="modal-2" class="modal">
  <div class="window">Modal Window 2<br /><a href="#">Close</a></div>
</div>
<div id="modal-3" class="modal">
  <div class="window">Modal Window 3<br /><a href="#">Close</a></div>
</div>
<div id="modal-4" class="modal">
  <div class="window">Modal Window 4<br /><a href="#">Close</a></div>
</div>
<div id="modal-5" class="modal">
  <div class="window">Modal Window 5<br /><a href="#">Close</a></div>
</div>
<a href="#modal-1">Open Modal 1</a><br />
<a href="#modal-2">Open Modal 2</a><br />
<a href="#modal-3">Open Modal 3</a><br />
<a href="#modal-4">Open Modal 4</a><br />
<a href="#modal-5">Open Modal 5</a>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252