Kevin Powell - CommunityKP-C
Kevin Powell - Community14mo ago
18 replies
Kivory

Error trying to change src of image using javascript

function changeImage(index) {
  console.log(`${index+1}`);

  var image1 = document.getElementById("img1");
  image1.src = `images/${index+1}/1.png`;
}

TypeError: image1 is null

What's going on here? console.log returns "1" just fine, which is the name of the proper directory.
Was this page helpful?