Forums

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

Home Forums JavaScript TypeError: document.getElementById(…)

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #191762
    wolfgang1983
    Participant

    On my JavaScript function previewImage I am trying to make the document.getElementById pick my row id up.

    Because on my addImage() function I have a segment

    Where the two Id id=”uploadPreview[‘ + image_row + ‘]” & id=”uploadImage[‘ + image_row + ‘]” should work with my previewImage().

    Error

    TypeError: document.getElementById(…) is null

    I cannot getElementById to work with the image_row.

    Codepen Sample “http://codepen.io/riwakawebsitedesigns/pen/NPRBZr”

    #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.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.