1

I want to click this image and for it to become larger:

This is how it's supposed to look after I click the image:

I placed the JavaScript code in another file to order things and now it isn't responding. it does nothing basically What I want is to click on an image and the image is brought up in a model of sorts. I will add images once I figure out how. each one of this are in a different file in the same folder.

This is the HTML code

<script type="text/javascript" src="JavaScript_formatting.js"></script> 
<link href="stylesheet.css" rel="stylesheet" type="text/css" />

<img id="myImg" style = "width: 480px; height: 384px;" src="7.jpg" alt="Image_wallpaper">
       <div id="myModal" class="modal">
            <span class="close">×</span>
    <img class="modal-content" id="img01">
        <div id="caption"></div>
    </div>

This is the css code

img id="myImg"

#myImg:hover {opacity: 0.7;}

/* The Modal (background) */
.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 */
#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

/* Add Animation */
.modal-content, #caption {  
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

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

@keyframes zoom {
  from {transform: scale(0.1)} 
  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;
}

This is the JavaScript code

// 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('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;
}

// 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";
}

Update: Using the answers provided I have tried what you suggested. However, when I click on the picture this happens which is not what I want:

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Jason West
  • 45
  • 1
  • 8

1 Answers1

3

Your code is unresponsive as you're trying to run your code before the page has fully loaded, meaning that your Javascript doesn't know about the elements in the DOM (document object model) and thus won't be able to function properly. So, you should run your code only when the page loads by using window.onload. Simpler, you can also bring the <script></script> tag to the bottom of the code since it runs from top to bottom. So, it loads that page then runs the script.

Also, you need to add an image source (using the src attribute) to your second image tag:

<img src="7.jpg" class="modal-content" id="img01">

See example below:

window.onload = function() { // add window.onload here and set it euqal to a function
  // 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('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;
  }

  // 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";
  }
} // close the function
#myImg:hover {
  opacity: 0.7;
}

/* The Modal (background) */
.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 */
#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

/* Add Animation */
.modal-content,
#caption {
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

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

@keyframes zoom {
  from {
    transform: scale(0.1)
  }
  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;
}
<img id="myImg" style="width: 200px; height: 184px;" src="https://media.wired.com/photos/5926db217034dc5f91becd6b/master/w_1904,c_limit/so-logo-s.jpg" alt="Image_wallpaper">
<div id="myModal" class="modal">
  <span class="close">×</span>
  <img src="https://media.wired.com/photos/5926db217034dc5f91becd6b/master/w_1904,c_limit/so-logo-s.jpg" class="modal-content" id="img01">
  <div id="caption"></div>
</div>
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • Thank you, i have one last question if you don't mind. why do i get an second image instead of the image being enlarged over the others. i'll post a picture. – Jason West Dec 31 '18 at 03:09
  • @JasonWest are you sure that you've linked the css correctly? What is the name of your css file? Is it in the same folder as your HTML file? – Nick Parsons Dec 31 '18 at 03:14
  • stylesheet.css and yes it is. everything worked until i have to add JavaScript that's when things started to brake. – Jason West Dec 31 '18 at 03:17
  • @JasonWest so if you add `* {border: 3px solid red;}` under `#myImg:hover { opacity: 0.7; }` in your `stylesheet.css` file do your images get a red borderer applied to them? – Nick Parsons Dec 31 '18 at 03:22
  • No, they do not – Jason West Dec 31 '18 at 03:26
  • @JasonWest that suggests to me that your css file isn't linking properly with your HTML. Try changing your `link` tag to: ``. Also I've updated my JS (you might want to try that as well) – Nick Parsons Dec 31 '18 at 03:28
  • @JasonWest [here](https://codesandbox.io/s/lyk2p4wp9q) is another working example with the files linked – Nick Parsons Dec 31 '18 at 03:31
  • Nothing changed i still have the x that's suppose to close the window under the image and it still makes a second image under the first. i coped and pasted what you have i truly don't know what i'm doing wrong. – Jason West Dec 31 '18 at 03:41
  • can you make your own [CodeSandbox](https://codesandbox.io/s/vanilla) code snippet (by copy and pasting your file contents into it) to try and recreate the issue? – Nick Parsons Dec 31 '18 at 03:42
  • Thank you, i figured it out codesandbox imported css in js – Jason West Dec 31 '18 at 03:47