 <p>I think you should use <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild" rel="nofollow">Node.replaceChild</a>.</p> <pre class="lang-js prettyprint-override"><code>var curImg = opener.document.getElementById("materiallogo"); var newImg = document.createElement("img"); // give it an id attribute newImg.setAttribute("id", curImg.id); var parentDiv = curImg.parentNode; // replace existing node curImg with the new img element newImg parentDiv.replaceChild(curImg, newImg); newImg.src = '' // &lt;-------------- HERE PUT NEW PATH FOR IMG </code></pre>