Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript TypeError: document.getElementById(…) Reply To: TypeError: document.getElementById(…)

#191822
GSutherland
Participant

With a quick glance, I’m not seeing the error you’re mentioning, but you do seem to have a pretty big problem. You can’t have more than one element with #preview, or more than one element with #image. IDs are only supposed to exist once on a page, hence why getElementById might not be working as you think it should. You probably want to change those to classes and use getElementsByClassName.

https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName

Also, I would use somewhat more specific names than ‘image’ and ‘preview’. If you’re going to put this on a page with more content, those are pretty generic class names and could apply to just about anything.