Search code examples
javascriptsimplemodal

Modal Image Issue in JavaScript


I'm trying to include an image modal into my photo gallery. I've been trying to work with a version of this example:

http://www-db.deis.unibo.it/courses/TW/DOCS/w3schools/howto/howto_css_modal_images.asp.html

The example only works with one image since it accesses the image through its ID. I've tried to modify it and just have it access the image through its class. But that doesn't work. How would I have to modify the JS code to make it work with any image in a gallery?

Thank you!!!

Modified code:

HTML:

<img class="galleryPic" src="assets/images/test images/globes.jpg" alt="Old globes" width="300" height="200">
<img class="galleryPic" src="assets/images/test images/palmtrees.jpg" alt="Palmtrees in front of the evening sky" width="300" height="200">

        <!-- The Modal -->
        <div id="myModal" class="modal">

            <!-- The Close Button -->
            <span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>
  
            <!-- Modal Content (The Image) -->
            <img class="modal-content" id="img01">
  
            <!-- Modal Caption (Image Text) -->
            <div id="caption"></div>
        </div>

JS:

// 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.getElementsByClass('galleryPic');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    modalImg.alt = this.alt;
    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";
}

Solution

  • There is not such thing as document.getElementsByClass(), there is document.getElementsByClassName() which returns a list of elements if found. So you'll need "loop" through the list:

    // 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.getElementsByClassName('galleryPic');
    var modalImg = document.getElementById("img01");
    var captionText = document.getElementById("caption");
    for (let i = 0; i < imgs.length; i++) {
      imgs[i].onclick = function() {
        modal.style.display = "block";
        modalImg.src = this.src;
        modalImg.alt = this.alt;
        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";
    }
    <img class="galleryPic" src="https://lh3.googleusercontent.com/9pPCK70Rw0k3wethMHb1qMaIB0VjeWLy57vYgSzKbF7oJuvO2nA0Nakk-95cvibWUDcEhYkfCKvdPKT03tXZd4M5jdhIEibLO9qw-XE=w1024-h683-n-l50-sg-rj" alt="Old globes" width="300" height="200">
    <img class="galleryPic" src="https://lh3.googleusercontent.com/taykG37GWDgY-FGkdogDvsHSJMUGRMvkuVRT6yR-5UNkKvGRKeRlpGYXlslocOcS0txlfUdGW59JGtzADknxbMqnh6AtVCv9EXyB8nHp80YsRNA0Yw=w1024-h683-n-l50-sg-rj" alt="Palmtrees in front of the evening sky" width="300" height="200">
    
    <!-- The Modal -->
    <div id="myModal" class="modal">
    
      <!-- The Close Button -->
      <span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>
    
      <!-- Modal Content (The Image) -->
      <img class="modal-content" id="img01">
    
      <!-- Modal Caption (Image Text) -->
      <div id="caption"></div>
    </div>