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?
Asked
Active
Viewed 423 times
-1
-
2Hello 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 Answers
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
-
Thank you for your answer. I already implemented multiple modals on the same page and they work fine. I only try to close them when clicking outside the modal window. The script I found in the mentioned post works fine, but only for one modal, cause it requires an id. (I cannot comment yet on that post cause this is a new account). – Alin Dec 22 '16 at 22:25
-
@Alin Okay, why not try my solution if that doesn't work out for you? This is flexible right? – Praveen Kumar Purushothaman Dec 22 '16 at 22:29
-
-
@Alin Why not use a JavaScript solution? You can actually do it, by using JavaScript. Can I add that in my solution? – Praveen Kumar Purushothaman Dec 22 '16 at 22:31
-
Yes please. I use java script (like in the post I mentioned) but it works only for one modal. – Alin Dec 22 '16 at 22:32
-
-
Works great, thank you so much. Also, Is it possible to achieve this when pressing ESC button? – Alin Dec 22 '16 at 22:41
-
@Alin Added Esc key support. `:D` Enjoy. Let me know if it works. – Praveen Kumar Purushothaman Dec 22 '16 at 22:43
-
1Excellent! Exactly as I need it. Thank you Praveen. Just realized I need to start learning java script asap. :) – Alin Dec 22 '16 at 22:48
-
-
I have one more Issue, hope you can help. When closing the modal window, it jumps on top of the page. Is there a way to maintain the scroll position? – Alin Dec 23 '16 at 18:33
-
@Alin That shouldn't happen, technically... Not sure why it happens for you. – Praveen Kumar Purushothaman Dec 23 '16 at 20:39
-
1I haven't made any changes to the script, only some css alterations to fit my needs. – Alin Dec 23 '16 at 20:57
-