I have a heading element created using document.createElement
:
let h1 = document.createElement("h1");
h1.innerHTML = "Heading"
I have an empty <div>
in the body too:
<div></div>
When I use document.body.appendChild(h1)
, it does as expected:
<h1>Heading</h1>
<div></div>
But I want to append the h1
element to both the <div>
and the body. The problem is that appendChild
moves the element if it is already appended.
The thing keeping me from modifying innerHTML
or cloning is that, lets say I want to change both the element's instances' innerHTML. So for that I need access to the element's DOM. And I may not know what is the exact position of the instance.
Does anyone know some other method to do this or a workaround to appendChild
?