When I need to retrieve the content string of a textNode, I typically use Text.wholeText instead of using toString or innerHTML because those methods won't work on objects.
For example: visit https://developer.mozilla.org/en-US/docs/Web/API/Text/wholeText
The read-only property Text.wholeText returns the full text of all Text nodes logically adjacent to the node, concatenated in document order. This allows you to specify any text node and get all nearby text as one string.
Syntax
str = textnode.wholeText;
Notes and example:
Imagine you have a simple paragraph in your webpage stored in a variable called para:
<p>Thru-hiking is great! <strong>No boring election coverage!</strong>
However, <a href="http://en.wikipedia.org/wiki/Absentee_ballot">casting a
ballot</a> is tricky.</p>
If you decide to remove the middle sentence, you can do so like this:
para.removeChild(para.childNodes[1]);
Later, if you want to change the wording to "Thru-hiking is great, but casting a ballot is tricky.", while keeping the hyperlink, you could try:
para.firstChild.data = "Thru-hiking is great, but ";
But be careful, if there are multiple adjacent text nodes, they may not behave as expected. Using wholeText helps to treat them as a single unit. For instance:
assert(para.firstChild.wholeText == "Thru-hiking is great! However, ");
The property wholeText combines the data of adjacent text nodes that are not separated by elements. Additionally, replaceWholeText() allows you to replace the entire text with new text:
para.firstChild.replaceWholeText("Thru-hiking is great, but ");
In some cases, Node.textContent or Element.innerHTML may be more appropriate than wholeText. However, when dealing with mixed content within an element, wholeText and replaceWholeText() can be useful tools.
For more information: https://developer.mozilla.org/en-US/docs/Web/API/Text/wholeText