0

In my script node1.data & node1.textContent are working perfectly but node1.innerHTML is not displaying anything, and I'm not getting why.

var text = [];
function getN(node1) {
    textNodesUnder(node1, text);
    return text.join("");
}
function textNodesUnder(node1, text) {
    if (node1.nodeType == 3) {
        text.push(node1.innerHTML);
        document.write(node1.textContent + "<br>");
    }
    var children = node1.childNodes;
    for (var i = 0; i < children.length; i++) {
        textNodesUnder(children[i], text);
    }
}

and

<body onload="alert('The document content is ' + getN(document))" >
j08691
  • 204,283
  • 31
  • 260
  • 272

3 Answers3

1

At that point node1 is a reference to the document object. Which indeed does not have the innerHTML property

leo.fcx
  • 6,137
  • 2
  • 21
  • 37
0

try document.body.innerHTML as it will give the element that has the inner HTML elements

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
0

The Element.innerHTML property sets or gets the HTML syntax describing the element's descendants. Example

document.getElementById("some_element").innerHTML="JavaScript"; //innerHTML is property of element not document

MDN - innerHTML

Useful information regarding JavaScript-innerHTML

Community
  • 1
  • 1
SK.
  • 4,174
  • 4
  • 30
  • 48