0

This code is working on JSFiddle but It does not work on my script also, I get this error with Firebug

Where is my mistake?

TypeError: document.getElementById(...) is null

document.getElementById("image").onclick = function() {
  if (this.src == "https://i.stack.imgur.com/5FoXm.png?s=48&g=1") {
    this.src = "https://i.stack.imgur.com/EoD62.jpg?s=32&g=1";
  } else {
    this.src = "https://i.stack.imgur.com/5FoXm.png?s=48&g=1";
  }
};
Cœur
  • 37,241
  • 25
  • 195
  • 267
Editor
  • 622
  • 1
  • 11
  • 24

2 Answers2

1

I added before </body> DOM and working. @Bartosz Gościński - I solved the problem with his idea

Community
  • 1
  • 1
Editor
  • 622
  • 1
  • 11
  • 24
0

You have added the line document.getElementById("image3").onclick = function() { twice in your code and the second time the name is imag3e instead of image3 which is not present in the DOM and it could be the reason you are getting the error. Remove the unnecessary line and try the following code.

document.getElementById("image").onclick = function() {
  if (this.src == "https://i.stack.imgur.com/5FoXm.png?s=48&g=1") {
    this.src = "https://i.stack.imgur.com/EoD62.jpg?s=32&g=1";
  } else {
    this.src = "https://i.stack.imgur.com/5FoXm.png?s=48&g=1";
  }
};
Joyson
  • 3,025
  • 1
  • 20
  • 34