Home › Forums › JavaScript › TypeError: document.getElementById(…) › Reply To: TypeError: document.getElementById(…)
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.