Node: normalize() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
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
js
normalize()
Parameters
None.
Return value
None.
Example
html
<output id="result"></output>
js
const wrapper = document.createElement("div");
wrapper.appendChild(document.createTextNode("Part 1 "));
wrapper.appendChild(document.createTextNode("Part 2 "));
let node = wrapper.firstChild;
let result = "Before normalization:\n";
while (node) {
result += ` ${node.nodeName}: ${node.nodeValue}\n`;
node = node.nextSibling;
}
wrapper.normalize();
node = wrapper.firstChild;
result += "\n\nAfter normalization:\n";
while (node) {
result += ` ${node.nodeName}: ${node.nodeValue}\n`;
node = node.nextSibling;
}
const output = document.getElementById("result");
output.innerText = result;
Specifications
Specification |
---|
DOM # ref-for-dom-node-normalize① |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
normalize |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
See also
Text.splitText()
, its opposite.