dom / latest / node / normalize.html /

Node.normalize()

The normalize() method of the Node interface 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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
normalize
1
12
1
9
≤12.1
1
1
18
4
≤12.1
1
1.0

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Node/normalize