Why the html content of an "img" element cannot be transferred to another element using innerHTML?

35 views Asked by At

I have two elements and I would like to transfer the html content of an element to the other one. However, for some reason that I don't understand at this moment, it doesn't work;

let firstElement = document.createElement('div'); 
let secElement = document.createElement('img'); 

secElement.innerHTML = "<img src = 'head.png'/>";
firstElement = secElement.innerHTML;

document.body.append(firstElement);

If I run this code, the image contained in the "secElement" and transferred into the "firstElement" is not shown. Also, if I check the "firstElement" in the console, its content is displayed as ''.

I also cannot use the "setAttribute()" method to add an attribute to the "firstElement" because I receive the error "Uncaught TypeError: firstElement.setAttribute is not a function".

Why is this happening?

Can you please help me?

Thank you very much.

0

There are 0 answers