I have 3 input[type="file"]
allow user upload 3 images, and user only can select 3 images on each input, but what I trying to implement is:
Consider user click on first input, now user can select 1 image, but if user select 2 or 3 image I want to send second or third image to next inputs. For example if user click on first input, and select 3 images, first image should set on first input, second should set on second input and also third to third input. Also if click second or third input, it should send each image to each input file.
function imgToData(input) {
if (input.files && input.files[0]) {
var File = new FileReader();
File.onload = function() {
var Img = new Image();
Img.onload = function() {
$('#' + input.id + '-val').val(Img.src);
$('#' + input.id + '-preview').attr('src', Img.src).css('visibility', 'visible').fadeIn();
};
Img.src = File.result;
};
File.readAsDataURL(input.files[0]);
}
}
$("input[type='file']").each(function() {
$(this).change(function() {
if (parseInt($(this).get(0).files.length) > 3) {
alert("You can only upload a maximum of 3 images");
} else {
imgToData(this);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="file" id="img-1" multiple/>
<input type="text" id="img-1-val" />
<img id="img-1-preview" width="30" />
</div>
<div>
<input type="file" id="img-2" multiple/>
<input type="text" id="img-2-val" />
<img id="img-2-preview" width="30" />
</div>
<div>
<input type="file" id="img-3" multiple/>
<input type="text" id="img-3-val" />
<img id="img-3-preview" width="30" />
</div>
This is what I tried so far:
$.each($(this)[0].files, function(i,v){
if(i==0){
// go to input 1
} else if(i==1){
// go to input 2
} else if(i==2){
// go to input 3
}
});
But no idea how should send value to each related input. Any idea?