How can i preview multiple images uploaded from different inputs?
The content from textarea is written inside div seeimg on button press, the textarea contains <img src="$img" id="img1">
,the id's are from 1 to 15 (img1,img2,img3...img15) and i want the images uploaded by user to be seen inside div seeimg
HTML
<form method="post" enctype="multipart/form-data" id="mainform">
Imagine 1:<br>
<input type="file" name="img1" id="img1"><br>
<br><br>
Imagine 2 :<br>
<input type="file" name="img2" id="img2"><br>
<br>
Imagine 3 :<br>
<input type="file" name="img3" id="img3"><br>
<br>
Imagine 4 :<br>
<input type="file" name="img4" id="img4"><br>
<br>
Imagine 5 :<br>
<input type="file" name="img5" id="img5"><br>
<br>
Imagine 6 :<br>
<input type="file" name="img6" id="img6"><br>
<br>
Imagine 7 :<br>
<input type="file" name="img7" id="img7"><br>
<br>
Imagine 8 :<br>
<input type="file" name="img8" id="img8"><br>
<br>
Imagine 9 :<br>
<input type="file" name="img9" id="img9"><br>
<br>
Imagine 10 :<br>
<input type="file" name="img10" id="img10"><br>
<br>
Imagine 11 :<br>
<input type="file" name="img11" id="img11"><br>
<br>
Imagine 12 :<br>
<input type="file" name="img12" id="img12"><br>
<br>
Imagine 13 :<br>
<input type="file" name="img13" id="img13"><br>
<br>
Imagine 14 :<br>
<input type="file" name="img14" id="img14"><br>
<br>
Imagine 15 :<br>
<textarea id="insert" name="content"></textarea>
<input type="file" name="img15" id="img15"><br>
</form>
<div id="seeimg">
</div>
So far the scripts it's showing just the image uploaded on first input file <input type="file" name="img1" id="img1">
,and if i keep changing the image of the first input it's changing the next instead of the first one.
Jquery
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
console.log(e.target.result+" , "+i);
$('#seeimg #img'+i).attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("form#mainform #img"+i).change(function(){
readURL(this);
i++;
});
TL;DR the scripts it's working just for the first file input and it keeps changing others img src if i upload another image.
Fiddle: https://jsfiddle.net/eLss3ojx/6/