var modal = document.getElementById("myModal");
var img = document.getElementsByClassName("img");
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";
}
.row{
 display: flex;
 flex-wrap: wrap;
 padding: 0 4px;
 justify-content: center;
}
.column{
 flex: 1 0 22%;
 max-width: 25%;
 padding: 0 4px;
}
.column img{
 margin-top: 8px;
 vertical-align: middle;
 width: 100%;
 box-shadow: 3px 3px 2px grey;
 border-radius: 8px;
}
.img {
    cursor: pointer;
    transition: 0.3s;
}
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}
/* Modal Content (Image) */
.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}
/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}
/* Add Animation - Zoom in the Modal */
.modal-content, #caption {
  animation-name: zoom;
  animation-duration: 0.6s;
}
@keyframes zoom {
  from {transform:scale(0)}
  to {transform:scale(1)}
}
/* The Close Button */
.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;
}
/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}
<!DOCTYPE html>
<html>
 <head>
  <title>Example</title>
 </head>
 <body>
  <div class="conta">
   <div class="row">
    <div class="column">
     <img class="img" alt="example" src="example.jpg">
     <img class="img" alt="example" src="example/example.jpg">
    </div>
    <div class="column">
     <img class="img" alt="example." src="example/example.jpg">
     <img class="img" alt="example"src="example/example.jpg">
     <img class="img" alt="example" src="example/example.jpg">
    </div>
    <div class="column">
     <img class="img" alt="example" src="example/example.jpg">
     <img class="img" alt="example" src="example/example.jpg">
     <img class="img" alt="example" src="example/example.jpg">
    </div>
    <div class="column">
     <img class="img" alt="example" src="example/example.jpg">
     <img class="img" alt="example" src="example/example.jpg">
     <img class="img" alt="example" src="example/example.jpg">
    </div>
   </div> 
  </div>
  <div id="myModal" class="modal">
   <span class="close">×</span>
   <img class="modal-content" id="img01">
   <div id="caption"></div>
  </div>
  <script src="lightBox.js"></script>
 </body>
</html>
I am trying to make a modal image gallery. I tried to make all the images with the class img. But nothing happens and I can't click the images. I get an error in my inspector. It is ' Cannot set property 'onclick' of undefined'. On the line  span.onclick = function() { I tried the two link methods below. Neither worked for me.
Modal image galleries - multiple images
https://www.w3schools.com/howto/howto_css_modal_images.asp
Is this only possible with query? I don't really want to use it.