How can I make a list of images clickable (modal images) in html?

tuba

I want to make a section of modal images but I don't know how to combine one modal image with another, and it doesn't seem to work when I copy-paste everything for every modal image and do it all separately either, or it's me who couldn't do it (I only just started learning html 2 days ago, sorry if this is common knowledge.)

This is what I tried, but it only works when the bold (**) codes are out, making it impossible for me to make multiple modal images in a page.

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";
  modalImg.src = this.src;
  captionText.innerHTML = this.alt;
}

var span = document.getElementsByClassName("close")[0];

span.onclick = function() { 
  modal.style.display = "none";
}
body {font-family: century;}

#myImg {
  border-radius: 10px;
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {opacity: 0.9;}

.modal {
  display: none; 
  position: fixed; 
  z-index: 1; 
  padding-top: 100px; 
  left: 0;
  top: 0;
  width: 100%; 
  height: 100%; 
  overflow: auto; 
  background-color: rgba(0,0,0,0.9); 
}

.modal-content {
  margin: auto;
  display: block;
  width: 50%;
  max-width: 400px;
  align-content: center;
}

#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

.modal-content, #caption {  
  animation-name: zoom;
  animation-duration: 0.6s;
}

@keyframes zoom {
  from {transform:scale(0)} 
  to {transform:scale(1)}
}

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<ul>
<li><img id="myImg" src="images/Husmand.jpg" alt="Husmand, 1900" style="width:100%;max-width:300px"> </li>
<li> <img id="myImg" src="images/Black Death, 1900.jpg" alt="Black Death, 1900" style="width:100%;max-width:300px"> </li>
</ul>

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




</body>
</html>

Jbadminton

Fixed it by using classes instead of Ids.
I gave all your images an myImg class
You should use a forEach loop for this so every item get his own onclick event.

var modal = document.getElementById("myModal");

document.querySelectorAll('.myImg').forEach(function(image) {
    var modalImg = document.getElementById("img01");
    var captionText = document.getElementById("caption");
    image.onclick = function(){
      modal.style.display = "block";
      modalImg.src = this.src;
      captionText.innerHTML = this.alt;
    }

    var span = document.getElementsByClassName("close")[0];

    span.onclick = function() { 
      modal.style.display = "none";
    }
});
body {font-family: century;}

.myImg {
  border-radius: 10px;
  cursor: pointer;
  transition: 0.3s;
}

.myImg:hover {opacity: 0.9;}

.modal {
  display: none; 
  position: fixed; 
  z-index: 1; 
  padding-top: 100px; 
  left: 0;
  top: 0;
  width: 100%; 
  height: 100%; 
  overflow: auto; 
  background-color: rgba(0,0,0,0.9); 
}

.modal-content {
  margin: auto;
  display: block;
  width: 50%;
  max-width: 400px;
  align-content: center;
}

#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

.modal-content, #caption {  
  animation-name: zoom;
  animation-duration: 0.6s;
}

@keyframes zoom {
  from {transform:scale(0)} 
  to {transform:scale(1)}
}

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<ul>
<li><img class="myImg" src="images/Husmand.jpg" alt="Husmand, 1900" style="width:100%;max-width:300px"> </li>
<li> <img class="myImg" src="images/Black Death, 1900.jpg" alt="Black Death, 1900" style="width:100%;max-width:300px"> </li>
</ul>

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




</body>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i make an output of multiple images with an for loop in processing?

How can i make custom border corners using images in css/html?

How to make images clickable to display a subset Dataframe ? (R Shiny )

How can I make these images stay in a "grid"?

How can I make images rows scale inside div

How can I make centering images in LibreOffice Impress easier?

How can I make a local repository of Ubuntu MAAS PXE Images?

How can I make a desktop wallpaper slideshow with my own images?

How to Set Up a List of Clickable Images with GridView

How can I make all inserted images in the textarea 100% wide

Basic HTML - Unordered List, how to make it my images vertical?

How to make swiper.js images clickable?

How can I make clickable list with sublinks ?

How can I make my DB images be displayed in the table?

HTML and CSS Based Gallery, Cannot make images clickable

How can I make these images same size using bootstrap?

i can not show the images in html

How can i captured detected images of face in opencv to make a database?

tappable Horizontal list view in flutter. how do i make a clickable list view with images from the list

How can I make div that contains images to have fixed size?

how can I make button align with my images

How can I make the whole HTML date field clickable?

How can I create two clickable intersecting Images?

How can I make my images responsive aligned in a scrollable row?

How can i adjust images , unordered list and buttons in html and css

How do I make <div> not cover images in HTML?

I can't list images in modal structure in Angular

How can I apply some animation for list of images in pygame?

How to make clickable images that acts like input file