0

I have a function that uses getElementById and it has been working great. Now I have a page where I call it more than once on the page. The first one works but the rest don't. What do I need to do to make multiples work on one page?

Link provided to other question doesn't answer my question. I'm not getting an error. It's just not doing anything when used more than once. It works on the first element, but not any others.

<script>
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";
}
</script>
Glenn
  • 35
  • 3
  • 1
    You need to show some code so we can see what is wrong. At a guess, have you given multiple elements the same id? if so, the getElementById will only get the first. Give the others a different id. – Leo Farmer Jul 27 '18 at 01:28
  • Will this not work with some sort of class so I can give them the same name? – Glenn Jul 27 '18 at 01:43

0 Answers0