
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element normalize() Method
The HTML DOM Element normalize() method is used to combine (or join) adjacent text nodes within an HTML element and removes any empty text nodes.
The term adjacent text nodes refers to text nodes that are located next to each other without any intermediate elements.
Syntax
Following is the syntax of the HTML DOM Element normalize() function −
node.normalize();
Parameters
This method does not accept any parameter.
Return Value
This method does not return any value.
Example 1: Combining the Adjacent Text Nodes
The following is the basic example of the HTML DOM Element normalize() function. It combines the adjacent text nodes −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element normalize()</title> </head> <body> <h3>HTML DOM Element normalize() Method</h3> <p>Combines the adjacent text nodes by removing extra whitespace.</p> <div id="pt"> <p>This is <em>some</em> text nodes</p> <div>-----spaces----</div> <p>Another text node with spaces</p> <div>---spaces----</div> <p>And <em>more </em> text nodes</p> </div> <div id="output"></div> <div style="margin-top: 20px;"> <button onclick="normalizeAndDisplay()">Normalize Content</button> </div> <script> function normalizeAndDisplay() { const pDiv = document.getElementById('pt'); pDiv.normalize(); const outputDiv = document.getElementById('output'); outputDiv.innerHTML = `<p><strong>Normalized HTML:</strong></p> ${pDiv.innerHTML}`; // Remove spacer elements from output const spacerElements = outputDiv.querySelectorAll('div'); spacerElements.forEach(element => { element.remove(); }); } </script> </body> </html>
The above program combines the adjacent text nodes within a div element by removing all the extra white spaces.
Example 2: Combining and Normalizing the Elements Dynamically
Following is another example of the HTML DOM Element normalize() method. We use this method to "combine" and "normalize" the content of an HTML element dynamically −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element normalize()</title> </head> <body> <h3>HTML DOM Element normalize() Method</h3> <p>Add Text Normalize</p> <p id="paragraph">Click one button to add text to this paragraph, click the other button to normalize the paragraph.</p> <div class="button-container"> <button onclick="addText()">Add Text</button> <button onclick="normalizeParagraph()">Normalize Paragraph</button> </div> <p id="output">The paragraph above has <span id="child-count">1</span> child node(s). </p> <script> function addText() { const paragraph = document.getElementById('paragraph'); const newText = document.createTextNode(' Additional text.'); paragraph.appendChild(newText); updateChildCount(); } function normalizeParagraph() { const paragraph = document.getElementById('paragraph'); paragraph.normalize(); updateChildCount(); } function updateChildCount() { const childCount = document.getElementById('paragraph').childNodes.length; document.getElementById('child-count').textContent = childCount; } </script> </body> </html>
After executing the above program, it combines and normalizes the element contents when the button is clicked.
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
normalize() | Yes | Yes | Yes | Yes | Yes |