Node.normalize()

The normalize() method of the Node puts the specified node and all of its sub-tree into a normalized form. In a normalized sub-tree, no text nodes in the sub-tree are empty and there are no adjacent text nodes.

Syntax

normalize();

Parameters

None.

Return value

None.

Example

<output id="result"></output>
let wrapper = document.createElement("div");

wrapper.appendChild( document.createTextNode("Part 1 ") );
wrapper.appendChild( document.createTextNode("Part 2 ") );

let node = wrapper.firstChild;
let result = "Before normalization:<br/>";
while (node) {
  result += " " + node.nodeName + ": " + node.nodeValue + "<br/>";
  node = node.nextSibling;
}

wrapper.normalize();

node = wrapper.firstChild;
result += "<br/><br/>After normalization:<br/>";
while (node) {
  result += " " + node.nodeName + ": " + node.nodeValue + "<br/>";
  node = node.nextSibling;
}

const output = document.getElementById("result");
output.innerHTML = result;

Specifications

Specification
DOM Standard (DOM)
# ref-for-dom-node-normalize①

Browser compatibility

BCD tables only load in the browser

See also