I want to preview images, that the user is about to upload. Now, I know that my code works for one input and one image. As soon as I add another pair of input and image, same as the one which worked, no image is there to preview...
How can I add more inputs, each with its own image to preview that specific input?
My code looks like this:
HTML:
<input type="file" accept="image/*" onchange="previewFile()">
<img id="output1" height="100px">
and JavaScript:
function previewFile(){
var preview = document.querySelector('img');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
}
else {
preview.src = "";
}
}
I think that a Var has to be given to the function, which output I want to choose, but I am totally new to Js, so maybe You can help me!
Thanks!
Edit:
I don't want to have a "multiple" attribute in my input tag. I created multiple divs, each with a non-visible input tag and an image that overlays with absolute position. So when the user clicks the div, he can choose an image, which then fills up the whole div.
It all works fine for one div but I need 11 more, each working separately, so that the user can manage the order of his pictures he is about to upload.
So what do I have to do to make each of the 12 upload tags working separately with its own preview image?
Edit:
I don't want to have a "multiple" attribute in my input tag. I created multiple divs, each with a non-visible input tag and an image that overlays with absolute position. So when the user clicks the div, he can choose an image, which then fills up the whole div.
It all works fine for one div but I need 11 more, each working seperately, so that the user can manage the order of his pictures he is about to upload.
So what do I have to do to make each of the 12 upload tags working seperately with its own preview image?
Element.id
should be unique in document
. You can pass the clicked element to previewFile()
call, use .nextElementSibling
to select sibling <img>
element
function previewFile(input) {
var preview = input.nextElementSibling;
var file = input.files[0];
var reader = new FileReader();
reader.onloadend = function() {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
}
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>
<div>
<input type="file" multiple accept="image/*" onchange="previewFile(this)">
<img height="100px" />
</div>