0

I have a table. Each row has one cell for image uploading. I want to display the preview. For this I wrote the code below:

var cell8 = row.insertCell(6);
var t8 = document.createElement("input");
t8.type = "file";
t8.id = "img" + index;
t8.name = "img" + index;

var a = document.getElementById('blah_img' + index + '');
a.src = window.URL.createObjectURL(this.files[0]);
t8.onchange = a.src;
cell8.appendChild(t8);

var oImg = document.createElement("img");
oImg.setAttribute('src', 'noimg.png');
oImg.setAttribute('alt', 'your image');
oImg.setAttribute('height', '100');
oImg.setAttribute('width', '100');
oImg.id = "blah_img" + index;
cell8.appendChild(oImg);

While running it gives me an error:

Uncaught TypeError: Cannot read property '0' of undefined.

What's wrong in my code? Please help.

Rose
  • 600
  • 5
  • 18

1 Answers1

0

Most probably, this.files is undefined. Thus it's giving the error. Instead of this.files[0], try t8.files[0]

Chobi is an Image Processing Library, implemented in pure Javascript which I have designed to make such things easier. You could check that out too

Jay Ghosh
  • 674
  • 6
  • 24