I'm a noob at coding but I am trying to make a team members' page where their image is clicked on and then another image pops up showing their description etc. I have copied a code from https://www.w3schools.com/howto/howto_css_modal_images.asp and then found a suggestion on StackOverflow in which I implemented their code onto mine. The only problem is I cannot get the other image to be clicked on and show a different image.
Here is the codepen: https://codepen.io/dm1010101/pen/wvmQPej Here is the JS code:
var modal = document.getElementById("myModal");
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
You are using same id on both images.
I used custom function
img_clickand passed the clicked element with it like thisonClick="img_click(this)"and now your both images clicks are working fine and showing alts saved on those images. And ofcourse Id's are not needed as you are passingthiswith your function calling.This this snippet.