I'm new with JQuery. I'm using the Silverstripe PHP. I've managed to get the on click event with multiple divs with the same classes but if I want have different image in the second button with same class and from the same ID. What is the solution for this problem?
I'm aware that it has to be different IDs for it to work but the each button as loop function so it has to be the same ID.
file.ss:
<div class="PosterButton">
<button class="posterBtn button" id="myBtn" value="$ClassPosterID">
<i class="fas fa-image"></i> View poster
</button>
</div>
<div class="myModal modal" id="myModal">
<div class="modal-content">
<span class="close">×</span>
<img src="image-1.jpg" class="modal-content" />
</div>
</div>
<div class="PosterButton">
<button class="posterBtn button" id="myBtn" value="$ClassPosterID">
<i class="fas fa-image"></i> View poster
</button>
</div>
<div class="myModal modal" id="myModal">
<div class="modal-content">
<span class="close">×</span>
<img src="image-2.jpg" class="modal-content" />
</div>
</div>
main.css:
.modal {
display: none;
position: fixed;
z-index:9999;
padding-top:60px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
.modal-content {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
jQuery.js:
var modal = document.getElementById('myModal');
var span = document.getElementsByClassName("close")[0];
$(document).ready(function() {
$('.posterBtn').click(function(event) {
event.preventDefault();
modal.style.display = "block";
});
});
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
The first button i clicked on is working fine when pops up with the first image but when i clicked on the second button, it works fine but pops up the same first ID with the first image. It should be showing the second ID with the second image.
What's the right way to get this working? am i doing it all wrong?