Reputation: 705
I am creating a div box which should hold the image a user uploads (to preview). However the div is created, but the image does not append to it. The ID IS being assigned to the div, but somehow I cannot target the DIV by ID. I have looked on stackoverflow, but didn't really find anything regarding this problem
Code:
function readURL(input) {
if (input.files && input.files[0]) {
$('#imagePreview').slideUp();
for (var i = 0; i < input.files.length; i++) {
let reader = new FileReader();
let card = document.createElement('div');
card.setAttribute('class', 'card col-md-4');
let header = document.createElement('div');
header.setAttribute('class', 'card-header');
let body = document.createElement('div');
body.setAttribute('class', 'card-body');
body.setAttribute('id', i);
$('#imgUploadBody').append($(card).append(header, body));
let img = new Image();
reader.onload = function(e) {
img.src = e.target.result;
$('#'+i).append(img);
}
reader.readAsDataURL(input.files[i]);
}
}
}
I have tried using different selectors, tried inserting the image directly in the body (directly appending it to it), the latter one does work, but the image does not care about its parent size. The image has no boundaries and takes its own width and height instead of respecting the parent div
its width and height.
So what I am looking for: The image should be appended to the div
(body) in a way that the image will respect the height and width of the parent div
.
It can be done in every way or shape. I am not bound to having to work with an ID as a selector. Just throwing it out there in case you have an idea which involves a different way of selecting the div
element
Upvotes: 1
Views: 24
Reputation: 13407
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit
Instead of making #0
make it #card0
Upvotes: 1