Element normalize Method - Javascript DOM

Javascript examples for DOM:Element normalize

Description

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

Parameters

None

Return Value:

No return value

The following code shows how to Normalize an element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#cc {//ww w . j a v  a  2s.com
    font-size: 20px;
    color: red;
}
</style>
</head>
<body>

<p id="demo">test</p>

<button onclick="addTextNode()">Add a Text Node</button>
<button onclick="normPara()">Normalize the paragraph</button>

<p>The paragraph above has <span id="cc">1</span> child node(s).</p>

<script>
function addTextNode() {
    var x = document.createTextNode(" Click again.");
    var y = document.getElementById("demo");
    y.appendChild(x);
    var x = document.getElementById("cc");
    x.innerHTML = y.childNodes.length;
}

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

</body>
</html>

Related Tutorials