HTML DOM normalize( ) Method

HTMLWeb DevelopmentFront End Technology

The HTML DOM normalize() method removes empty Text Nodes, and joins adjacent Text Nodes from a specified node.

Following is the syntax −

Calling normalize()

document.normalize()

Let us see an example of normalize() method −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM normalize()</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
   <form>
      <fieldset>
         <legend>HTML-DOM-normalize( )</legend>
         <input type="text" id="textSelect" placeholder="type here...">
         <input type="button" onclick="makeTextNode()" value="Create Text Node">
         <input type="button" onclick="normalizeDocument()" value="Normalize">
         <div id="appendedNodes">All Text Nodes: </div>
         <div id="divDisplay"></div>
      </fieldset>
   </form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var textSelect = document.getElementById("textSelect");
   var appendedNodesDiv = document.getElementById("appendedNodes");
   divDisplay.textContent = 'Total Text Nodes: '+appendedNodesDiv.childNodes.length;
   function makeTextNode() {
      var textNode = document.createTextNode(textSelect.value);
      appendedNodesDiv.appendChild(textNode);
      if(textSelect.value === '')
         divDisplay.textContent = 'Empty Text Node Created';
      else
         divDisplay.textContent = 'Text Node Created with value: '+textSelect.value;
         divDisplay.textContent += ', Total Text Nodes: '+appendedNodesDiv.childNodes.length;
      }
   function normalizeDocument() {
      appendedNodesDiv.normalize();
      divDisplay.textContent = 'Total Text Nodes: '+appendedNodesDiv.childNodes.length;
   }
</script>
</body>
</html>

Output

Before clicking any button −

After clicking ‘Create Text Node’ button with empty input −

After clicking ‘Create Text Node’ button with non-empty input −

After clicking ‘Normalize’ button −

After clicking ‘Normalize’ button −

raja
Published on 25-Oct-2019 08:07:35
Advertisements