In the W3C DOM (Document Object Model) , everything is a βnodeβ. Nodes come in different types (comment nodes, element nodes, attribute nodes, and even text nodes). It may seem inconsistent that an element such as a div , which does not have nested elements that can contain text inside it, actually implicitly has a child element inside it that contains raw text, and this element is text node.
To access this (which will be separated from other elements in the div , you can go to the div and search (in this case, it is firstChild , because the text comes first and the image is second.
Also, when it comes to replacing the source text with something else ... You tried to call the .replace() string in a div , not text in a div . You can select only the div text by going to the node text inside it and working on this very issue.
function change_stuff() {
<div id="to_change"> This is a huge block of text that I want to replace while leaving the image in place <img src="./the_image.jpg"> </div> <button onclick="change_stuff();"> ThE dOER!! </button>
source share