'How to simplify this modal code with loop in javascript

I am trying to open a modal when an image is opened. The code here is working but I want to find a way to simplify this or shortened the code just in case there are more images with modals to be opened in the future.

I am thinking if the loop can be used to simplify this but I am very new to Javascript and still have a lot to learn that's why I need help on this.

<h2>Travel Blog</h2>
<div class="blogThumb">
<!--Thumbnails for blog-->
     <div class="blogformat">
      <img class="blogPhoto" id="myImg1" src="/img/thumb1.jpg"  alt="Italy">
     </div>
     <div class="blogformat">
      <img class="blogPhoto" id="myImg2" src="/img/thumb2.jpg"  alt="Venice">
     </div>
     <div class="blogformat">
      <img class="blogPhoto" id="myImg3" src="/img/thumb3.jpg"  alt="Philippines">
     </div>

<!-- The Modal-->
     <div id="myModal" class="modal">
      <span class="close">&times;</span>
      <img class="modal-content" id="img01">
      <div id="caption"></div>
     </div>


     <script>
     // Get the modal
     var modal = document.getElementById("myModal");
                          
     // Get the image and insert it inside the modal - use its "alt" text as a caption
     var img = document.getElementById("myImg1");
     var img2 = document.getElementById("myImg2");
     var img3 = document.getElementById("myImg3");
     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;
           }

     img2.onclick = function(){
           modal.style.display = "block";
           modalImg.src = this.src;
           captionText.innerHTML = this.alt;
           }

     img3.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>
                          
</div>


Solution 1:[1]

Get all the images, add an event listener and the set whatever needed with the target element - which is clicked one

<h2>Travel Blog</h2>
<div class="blogThumb">
<!--Thumbnails for blog-->
     <div class="blogformat">
      <img class="blogPhoto" id="myImg1" src="/img/thumb1.jpg"  alt="Italy">
     </div>
     <div class="blogformat">
      <img class="blogPhoto" id="myImg2" src="/img/thumb2.jpg"  alt="Venice">
     </div>
     <div class="blogformat">
      <img class="blogPhoto" id="myImg3" src="/img/thumb3.jpg"  alt="Philippines">
     </div>

<!-- The Modal-->
     <div id="myModal" class="modal">
      <span class="close">&times;</span>
      <img class="modal-content" id="img01">
      <div id="caption"></div>
     </div>


     <script>
     // Get the modal
     var modal = document.getElementById("myModal");
                          
     // Get the image and insert it inside the modal - use its "alt" text as a caption
     var imgs = document.querySelectorAll("img");
     var modalImg = document.getElementById("img01");
     var captionText = document.getElementById("caption");
     
     // get all images and add an event listener, event target will be the clicked one
     imgs.forEach(img=>img.addEventListener("click",
       function (e) {
         modal.style.display = "block";
         modalImg.src = e.target.src;
         captionText.innerHTML = e.target.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>
                          
</div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Codenewbie