0

Need to process text inside uncertain layers of nested nodes using following function:

function processTextNodesIn(elem) {
  if (elem) {
    for (var nodes = elem.childNodes, i = nodes.length; i--;) {
      var node = nodes[i], nodeType = node.nodeType;
      if (nodeType == 3) {
        console.log("innerHtml = " + node.innerHTML);
      }
    }
    else if (nodeType == 1 || nodeType == 9 || nodeType == 11) {
      processTextNodesIn(node);
    }
  }
}

} Text could be inside multiple <p></p> , <div></div> , <li></li> . But the content found is all "undefined". What did I do wrong?

Update: Tried innerText(textContent as well) as @patrick suggested, still prints undefined.

Daolin
  • 614
  • 1
  • 16
  • 41
  • Your code currently results in a SyntaxError. Can you make sure you post your actual code? – CertainPerformance Nov 24 '21 at 01:14
  • 1
    [Text nodes](https://developer.mozilla.org/en-US/docs/Web/API/Text) do not have an `innerHTML` property, to get the text use `textContent` – Patrick Evans Nov 24 '21 at 01:16
  • Can you not get the text content of all nested elements using `elem.textContent` of the parent element? – fubar Nov 24 '21 at 01:16
  • textContent and innerText behave the same. I would like to make some of text by add html formating to text. – Daolin Nov 24 '21 at 01:41

0 Answers0