Recently I started developing my own website and improving my own knowledge of coding. With this I already started to run into many problems that are taking quite a while to fix, but so far everything as been resolved except this one.
Yesterday I attempted to add "modal images" from w3schools to the picture gallery I created, everything seem to be working except the scripts.
Then I decided to isolate the script in a completly seperate file to make sure the code actually worked and it wasn't me that was doing something wrong. At the start it did work when the entire code was all jammed up in the HTML, after I seperate the CSS from HTML which caused no issues, but as soon as I took the scripts out of HTML into Scripts.JS the modal stopped working completly.
As my knowledge of javascript is pretty close to none I am hoping somebody can shine some light in my problem since I'm pretty out of ideas.
Here is the script.
PS: currently this is the only script that does not work and I have other scripts that do indeed work in the same file so the file is getting loaded its just this particular script isn't working properly.
// JavaScript Document
// 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.href ="";
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";
};