It seems like the issue lies in how you're handling the variable names modal
, btns
, and span
. One way to address this is by renaming those variables, but an even better approach would be to optimize your code for efficiency.
To solve the problem, I modified the id of the first modal by adding a suffix of #1. This way, we can easily link the button's id to the corresponding modal.
I've added comments throughout the code for clarity:
// Get all buttons with the class 'pack.detail'
var btns = document.querySelectorAll('.pack.detail');
// Get all the close spans
var span = document.querySelectorAll('span.close');
// Using original forEach loop
[].forEach.call(btns, function(el) {
el.onclick = function() {
// Retrieve the button's id
var id = el.id;
// Find the modal based on id
var modal = document.getElementById('modal' + id);
// Display the correct modal
modal.style.display = "block";
}
});
// Handle clicks on close spans
[].forEach.call(span, function(el) {
el.onclick = function() {
// Find the closest parent element with class 'modal'
var current_modal = el.closest('.modal');
// Hide the modal
current_modal.style.display = "none";
}
});
// Close modal on window click if target has class 'modal'
window.onclick = function(event) {
if (event.target.className == 'modal') {
event.target.style.display = "none";
}
}
/* Modal styling */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.4);
-webkit-animation-name: fadeIn;
-webkit-animation-duration: 0.4s;
animation-name: fadeIn;
animation-duration: 0.4s
}
/* Content styling */
.modal-content {
position: fixed;
bottom: 0;
background-color: #fefefe;
width: 100%;
-webkit-animation-name: slideIn;
-webkit-animation-duration: 0.4s;
animation-name: slideIn;
animation-duration: 0.4s
}
/* Close Button styling */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {
padding: 2px 16px;
}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
<button class="pack detail" id="1">Open modal 1</button>
<button class="pack detail" id="2">Open modal 2</button>
<!-- Modal 1 -->
<div id="modal1" class="modal">
<div class="modal-content">
<!-- Header -->
<div class="modal-header">
<span class="close">×</span>
<h2>Modal Header</h2>
</div>
<!-- Body -->
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
<!-- Footer -->
<div class="modal-footer">
<h3>Modal Footer</h3>
</div>
</div>
</div>
<!-- Modal 2 -->
<div id="modal2" class="modal">
<div class="modal-content">
<!-- Header -->
<div class="modal-header">
<span class="close">×</span>
<h2>Modal Header2</h2>
</div>
<!-- Body -->
<div class="modal-body">
<p>Some text in the Modal Body2</p>
<p>Some other text...</p>
</div>
<!-- Footer -->
<div class="modal-footer">
<h3>Modal Footer2</h3>
</div>
</div>
</div>