Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM createDocumentFragment() method

HTML DOM createDocumentFragment() method

Document Object Reference Document Object

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:

After modification:

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

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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 Reference Document Object