this is a js snippet of code that’s been giving me problems. I’m attempting to append 2 img layers with different dynamically generated HTML tags but browsers refuse to render the code correctly.
the desired outcome is for
dropzone.append(img);
dropzone.append(imgIn);
append 2 images with different 2 indexes and do this hundreds of times in a mosaic
here is a link to the android app this code is for https://www.webintoapp.com/store/107768
function hueChange() {
for(let image = 0; image < mosaicCount; image++) {
let screenWidth = window.innerWidth;
reader.addEventListener('loadend', () => {
let img = document.createElement('img');
let imgIn = document.createElement('imgIn');
img.setAttribute("id", image)
img.setAttribute("width", screenWidth/16 + "px")
imgIn.setAttribute("id", "imgIn" + image)
imgIn.setAttribute("width", screenWidth/16 + "px")
img.src = reader.result;
imgIn.src = reader.result;
dropzone.append(img); //this doesn't append
dropzone.append(imgIn); //this appends
3
Answers
You could create just one element and clone it when you append. Then update the original element and append that.
There’s no such thing as an
imgIn
element, so that element doesn’t render. You should useimg
for both elements.You should put the loop inside the event listener, instead of adding multiple listeners that all do the same thing.
Firs problem is you are creating a "custom" element since you are creating an element with "imgIn" so that is not going to render and image.
You can use cloneNode and alter the id so you do not have to duplicate id.