I’m working on uploading multiple images one at a time by dynamically generating the input field and image element. However, my code doesn’t display the images on the dynamically generated image element.
JavaScript
x
4
1
<button type="button" class="btn btn-sm btn-info add-image"><i class="ti-image"></i> Add image</button>
2
<br><br>
3
<div class="images"></div>
4
JavaScript
1
39
39
1
$(document).ready(function() {
2
var max_image = 10;
3
var count = 1;
4
5
$('.add-image').click(function(e) {
6
e.preventDefault();
7
if (count < max_image) {
8
count++;
9
$('.images').append(`<div class="input" style="width:100px;height:120px;border:2px dashed lightgrey;float:left;margin:8px">
10
<input type ="file" class ="fileup 1" id ="file'+count+'" style="width:100%; height:100%; opacity:0; position: absolute">
11
<img id ="image'+count+'" src="" style="width:100%; height:100%;">
12
<span class="btn btn-sm btn-danger delete" style="position:relative;bottom:20px"><I class="ti-trash"></i></span>
13
</div>`);
14
15
$(document).on('change', '#file' + count, function() {
16
readURL(this);
17
});
18
19
function readURL(input) {
20
if (input.files && input.files[0]) {
21
var reader = new FileReader();
22
reader.onload = function(e) {
23
$('#image' + count).attr('src', e.target.result);
24
}
25
reader.readAsDataURL(input.files[0]);
26
}
27
}
28
} else {
29
alert("Only a maximum of 10 images is allowed");
30
}
31
});
32
33
$('.images').on("click", ".delete", function(e) {
34
e.preventDefault();
35
$(this).parent('div').remove();
36
y--;
37
})
38
});
39
Advertisement
Answer
Instead of making event handler for all files use can just one event handler and then inside your readURL
function use .closest('div').find('img')
to add src to image tag.
Demo Code :
JavaScript
1
55
55
1
$(document).ready(function() {
2
// allowed maximum input fields
3
var max_image = 10;
4
5
// initialize the counter for textbox
6
var count = 1;
7
8
// handle click event on Add More button
9
$('.add-image').click(function(e) {
10
11
e.preventDefault();
12
13
if (count < max_image) {
14
15
count++; // increment the counter
16
17
// validate the condition
18
19
$('.images').append(`<div class="input" style="width:100px;height:120px;border:2px dashed lightgrey;float:left;margin:8px">
20
21
<input type ="file" class ="fileup 1" id ="file'+count+'" style="width:100%; height:100%; opacity:0; position: absolute">
22
23
<img id ="image'+count+'" src="" style="width:100%; height:100%;">
24
25
<span class="btn btn-sm btn-danger delete" style="position:relative;bottom:20px"><I class="ti-trash"></i></span>
26
27
</div>`); // add input field
28
} else {
29
alert("Only a maximum of 10 images is allowed");
30
}
31
});
32
33
// handle click event of the remove link
34
$('.images').on("click", ".delete", function(e) {
35
e.preventDefault();
36
$(this).parent('div').remove(); // remove input field
37
y--; // decrement the counter
38
})
39
40
//put this outside..
41
$(document).on('change', '.images input[type=file]', function() {
42
readURL(this);
43
});
44
45
function readURL(input) {
46
if (input.files && input.files[0]) {
47
var reader = new FileReader();
48
reader.onload = function(e) {
49
//get closest div and then find img add img there
50
$(input).closest('div').find('img').attr('src', e.target.result);
51
}
52
reader.readAsDataURL(input.files[0]);
53
}
54
}
55
});
JavaScript
1
4
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<button type="button" class="btn btn-sm btn-info add-image"><i class="ti-image"></i> Add image</button>
3
<br><br>
4
<div class="images"></div>