HTML DOM createDocumentFragment() method
Example
Create a documentFragment node:
var d=document.createDocumentFragment();
d.appendChild(document.getElementsByTagName("LI")[0]);
d.childNodes[0].childNodes[0].nodeValue="Milk";
document.getElementsByTagName("UL")[0].appendChild(d);
Output result:
Before modification:
- Coffee
- Tea
After modification:
- Tea
- Milk
Try it »
Definition and Usage
The createdocumentfragment() method creates a virtual node object that contains all properties and methods.
When you want to extract a part of the document, change, add, or delete some content, and insert it at the end of the document, you can use the createDocumentFragment() method.
You can also use the document's document object to perform these changes, but to prevent the document structure from being corrupted, the createDocumentFragment() method can more safely change the structure and nodes of the document.
Browser support





All major browsers support the createDocumentFragment() method
Syntax
document.createDocumentFragment()
Parameters
None.
Return Value
| Type | Description |
|---|---|
| DocumentFragment object | Create a document fragment object |
Technical Details
| DOM version | Core Level 1 Document Object |
|---|
Document Object