Is there any way to get the collection of all textNode
objects within a document?
getElementsByTagName()
works great for Elements, but
after createTreeWalker
is deprecated you can use
/**
* Get all text nodes under an element
* @param {!Element} el
* @return {Array}
*/
function getTextNodes(el) {
const iterator = document.createNodeIterator(el, NodeFilter.SHOW_TEXT);
const textNodes = [];
let currentTextNode;
while ((currentTextNode = iterator.nextNode())) {
textNodes.push(currentTextNode);
}
return textNodes;
}