You seem to be going about this the long way wit a whole lot more code than is needed for moving any element of a web page from one spot to another.
Assuming that the variable 'a' references your image - either already in the web page or created and waiting to be inserted and 'b' points to the element in the page you want to point it to then:
b.parentNode.insertBefore(a,b);
will insert the image immediately before the opening tag that 'b' points to and
b.appendChild(a);
will insert the image immediately before the closing tag of the element 'b' points to.
If the image was already in the page then either of those commands will remove it from where it was and insert it into the new location.
These commands will work for moving ANY element from one spot in the web page to another and not just images. |