I am trying to get the image id when an image is clicked and set it as a var. When the first image in the column is clicked it works. When any other image is clicked nothing happens. I've tried multiple different click events and can't get it working. Any leads or suggestions much appreciated.
<div class="row">
<div class="column">
<img id="myImg" alt="" src="img/work/3.jpg">
</div>
<div class="column">
<img id="myImg1" alt="" src="img/work/4.jpg">
</div>
<div class="column">
<img id="myImg2" alt="" src="img/work/6.jpg">
</div>
</div>
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
<script type="text/javascript">
var img = document.getElementById("myImg");
document.addEventListener('click', function(e) {
img = document.getElementById(e.target.id);
}, false);
var modal = document.getElementById("myModal");
//Get the image and insert it inside the modal - use its "alt" text as a
caption
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
//Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
//When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
</script>