Modal launch from several btns
Launching the same modal from several buttons with the same id
by LyndseyB
HTML
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<div class="modal-inner">THIS IS MY MODAL CONTENT</div>
</div>
</div>
<div class="linkholder">
<div class="bookbox">
<div class="bookbtn">BOOK NOW</div>
</div>
</div>
<div class="linkholder">
<div class="bookbox">
<div class="bookbtn">BOOK NOW</div>
</div>
</div>
<div class="linkholder">
<div class="bookbox">
<div class="bookbtn">BOOK NOW</div>
</div>
</div>
<div class="linkholder">
<div class="bookbox">
<div class="bookbtn">BOOK NOW</div>
</div>
</div>
<div class="linkholder">
<div class="bookbox">
<div class="bookbtn">BOOK NOW</div>
</div>
</div>
<div class="linkholder">
<div class="bookbox">
<div class="bookbtn">BOOK NOW</div>
</div>
</div>
CSS
.bookbtn {
position: relative;
margin: 8px auto;
padding: 8px;
min-height: 8px;
width: 50%;
border-radius: 8px ;
-moz-border-radius: 8px ;
-webkit-border-radius: 8px ;
background-color: rgba(0, 0, 0, 0.8);
color: #fff;
border: 2px solid #ffffff;
text-align: center;
z-index: 1;
}
.bookbtn:hover {
background: rgba(255, 255, 255, 0.8);
color: #000;
cursor: pointer;
border: 2px solid #FC461F;
}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 999; /* Sit on top */
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.9); /* Black w/ opacity */
}
/* Modal Content/Box */
.modal-content {
background-color: transparent;
margin: 4% auto; /* 4% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 88%; /* Could be more or less, depending on screen size */
min-height: 380px;
border: 1px solid #FC461F;
}
/* The Close Button */
.close {
color: #FC461F;
float: right;
font-size: 40px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #fff;
text-decoration: none;
cursor: pointer;
}
.modal-inner {
background-color: transparent;
margin: 10% auto;
padding: 60px;
width: 88%;
min-height: 380px;
border: 1px solid yellow;
color: #fff;
text-align: center;
}
JavaScript
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.querySelectorAll(".bookbox");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
[].forEach.call(btn, function(button) {
button.addEventListener('click', 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";
}
}