Javascript Reference - HTML DOM normalize Method








The normalize() method removes empty Text nodes, and joins adjacent Text nodes.

Browser Support

normalize Yes Yes Yes Yes Yes

Syntax

node.normalize()

Parameters

None.

Return Value

No return value.





Example

The following code shows how to normalize a document.


<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<button onclick="addTextNode()">Add a Text Node</button>
<button onclick="normPara()">Normalize the Document</button>
<script>
function addTextNode()<!--from  w  w w  .j  av  a  2  s.  c o  m-->
{
    var y=document.createTextNode(" Click again");
    var x=document.getElementById("demo");
    x.appendChild(y);
    var z=document.getElementById("mySpan");
    z.innerHTML=x.childNodes.length;
}

function normPara()
{
    document.normalize();
    var x=document.getElementById("demo");  
    var z=document.getElementById("mySpan");
    z.innerHTML=x.childNodes.length;
}
</script>

<p>this is a <span id="mySpan"> test </span>.</p>

</body>
</html>

The code above is rendered as follows: