0

So I am having a strange issue with JavaScript where one thing works and the other doesn't.

<img id="img_1" src="#" />
<img id="img_2" src="#" />

Example code:

  function changeName(id, name) {
    var image = name.files;

    $('#image_' + id).attr('data-title', image[0].name); // this works 
    $('#img_' + id).src = URL.createObjectURL(image[0]); // this doesn't
  }

This is what I tried and it works, but this obviously doesn't work for multiple IMG fields:

img_1.src = URL.createObjectURL(image[0]); // seems to work just fine

What am I trying to accomplish?

So the idea is that you get to preview the image before you upload it to the server. I have multiple upload fields so there will be multiple parts where you get to preview the image (it's for a blog).

I feel really dumb for asking why this doesn't work. Is this just a stupid brainfart or am I missing something?

Any idea? your help is much appreciated :)

Niels
  • 1,005
  • 1
  • 8
  • 18

0 Answers0