Multiple Modals One Script
by UltimateNoob
HTML
<a id="btnB12" class="btnB12" href="#B12">B12</a>
<a id="btnB02" class="btnB02" href="#B02">B02</a>
<div id="modalB12" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<p>B12 Modal</p>
</div>
</div>
<div id="modalB02" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<p>B02 Modal</p>
</div>
</div>
CSS
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
button {
cursor: pointer;
}
}
JavaScript
var modal = "",
lnk = "",
lnkNumb = "",
modalID = "",
lnkID = "",
span = document.getElementsByClassName("close")[0];
$('[class^="lnk"]').on('click', function(e) {
e.preventDefault();
lnkNumb = this.className.replace('lnk', '');
lnkID = ('lnk' + lnkNumb);
lnk = document.getElementById(lnkID);
modalID = ('modal' + lnkNumb);
modal = document.getElementById(modalID);
});
// When the user clicks the Link, open the modal
lnk.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}