
- 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 Document createDocumentFragment() Method
The createDocumentFragment() method creates a new empty document fragment which resides in memory. It acts like an empty container which holds the DOM elements before inserting into HTML document.
Elements are appended in the newly created document fragment and then this document fragment is appended to DOM tree where document fragment is replaced by all its children. It helps in changing the document's content by adding deleting or modifying a node.
Syntax
document.createDocumentFragment();
Parameter
This method does not accept any parameter.
Return Value
It returns the created document fragment node.
Examples of HTML DOM Document 'createDocumentFragment()' Method
The following examples illustrates use of createDocumentFragment method to add and modify existing elements in list.
Adding Elements to an Empty List
In the following example we will add an element in an empty list by using create createDocumentFragment() method.
<!DOCTYPE html> <html lang="en"> <head> <title> HTML DOM Document createDocumentx() Method </title> </head> <body> <h3>HTML DOM Document createDocumentFragment() Method</h3> <ul id="list"></ul> <script> let list = document.getElementById('list'); let x = document.createDocumentFragment(); let cars = ["Tata", "BMW", "Audi"]; for(const i of cars){ let item=document.createElement("li"); item.textContent=i; x.appendChild(item); } list.appendChild(x); </script> </body> </html>
Modifying the Existing List
In the following example we will modify an existing list using createDocumentFragment() method.
<!DOCTYPE html> <html lang="en"> <head> <title> HTML DOM Document createDocumentFragment() Method </title> </head> <body> <h3>HTML DOM Document createDocumentFragment() Method</h3> <button onclick="fun()">Click me</button> <ul> <li>BMW</li> <li>Tesla</li> <li>Audi</li> <li>Mahindra</li> <li>Honda</li> </ul> <script> function fun() { let x= document.createDocumentFragment(); x.appendChild(document.getElementsByTagName("li")[0]); x.childNodes[0].childNodes[0].nodeValue ="Tata"; document.getElementsByTagName("ul")[0].appendChild(x); } </script> </body> </html>
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
createDocumentFragment() | Yes 1 | Yes 12 | Yes 1 | Yes 1 | Yes 12.1 |