The element method normalize() used to remove empty text nodes, and joins adjacent text nodes. This method is also available in document object to normalize entire document.
Element method normalize()
1 <!DOCTYPE html>
2 <html>
3
4 <body onload="render()">
5
6 <h1>The Element Object</h1>
7 <h2>The normalize() method</h2>
8
9 <button onclick="addTextNode()">Add a Text Node</button>
10 <button onclick="render()">Normalize</button>
11 <div id="container">
12 <p>The element has <span id="counter">child node(s).</p>
13 </div>
14 <script>
15 const container = document.getElementById("container");
16 const counter = document.getElementById("counter");
17
18 function addTextNode() {
19 const textNode= document.createTextNode("Added new element !");
20 container.appendChild(textNode);
21 counter.innerHTML = container.childNodes.length;
22 }
23
24 function render() {
25 container.normalize();
26 counter.innerHTML = container.childNodes.length;
27 }
28 </script>
29 </body>
30 </html>
In the above example, a document is created with elements that includes script element. A script element retrieves two elements and define two functions, a function addTextNode() to add text node and normalize() to normalize element that will be display on UI on button click.
Element method normalize()
Related options for your search