Node Interface

In the DOM, all document parts are organized in a hierarchical tree-like structure consisting of parents and children. These individual parts are called Nodes. The Node interface is an abstract base class, so there is no such thing as a plain Node object. Look at some of the most used properties nodeType, parentNode, childNodes, firstChild, lastChild, previousSibling, nextSibling, and attributes.

Examples

Remove all children nested within a node

This function remove each first child of an element, until there are none left.

function removeAllChildren(element) {
  while (element.firstChild) {
    element.removeChild(element.firstChild);
  }
}

Using this function is a single call. Here we empty the body of the document:

removeAllChildren(document.body);

An alternative could be to set the textContent to the empty string: document.body.textContent = "".


Recurse through child nodes

The following function recursively calls a callback function for each node contained by a root node (including the root itself):

function eachNode(rootNode, callback) {
  if (!callback) {
    const nodes = [];
    eachNode(rootNode, (node) => {
      nodes.push(node);
    });
    return nodes;
  }

  if (callback(rootNode) === false) {
    return false;
  }

  if (rootNode.hasChildNodes()) {
    for (const node of rootNode.childNodes) {
      if (eachNode(node, callback) === false) {
        return;
      }
    }
  }
}

The function recursively calls a function for each descendant node of rootNode (including the root itself).

If callback is omitted, the function returns an Array instead, which contains rootNode and all nodes contained within.

If callback is provided, and it returns false when called, the current recursion level is aborted, and the function resumes execution at the last parent's level. This can be used to abort loops once a node has been found (such as searching for a text node which contains a certain string).

The function has two parameters:

rootNode

The Node object whose descendants will be recursed through.

callback Optional

An optional callback function that receives a Node as its only argument. If omitted, eachNode returns an Array of every node contained within rootNode (including the root itself).

The following demonstrates a real-world use of the eachNode() function: searching for text on a web-page.

We use a wrapper function named grep to do the searching:

function grep(parentNode, pattern) {
  let matches = [];
  let endScan = false;

  eachNode(parentNode, (node) => {
    if (endScan) {
      return false;
    }

    // Ignore anything which isn't a text node
    if (node.nodeType !== Node.TEXT_NODE) {
      return;
    }

    if (typeof pattern === "string" && node.textContent.includes(pattern)) {
      matches.push(node);
    } else if (pattern.test(node.textContent)) {
      if (!pattern.global) {
        endScan = true;
        matches = node;
      } else {
        matches.push(node);
      }
    }
  });

  return matches;
}