You do not need to update myCanvas
if it is still the same node. When you create a node and add it to the DOM, then the DOM node is live. This means that all changes to myCanvas
will be immediately displayed on the page.
replaceChild ()
If you want to replace the node with other nodes, you can use .replaceChild()
in the parent element of the node you want to replace.
Example:
document.getElementById("parent").replaceChild(element, newElement);
Where parent
is the parent element element
.
<div id="parent"> <canvas id="element"></canvas> </div>
innerHTML
In your question, you are using innerHTML
. If you want to simply replace the contents of one element with the contents of another element, use innerHTML
for both of them.
Example:
document.getElementById("element").innerHTML = newElement.innerHTML;
source share