THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

HTML DOM createDocumentFragment() Method

Document Object Reference Document Object

Example

Create a documentFragment node and append a child to it (a list item). Then, change the list item's node value and insert it as the last child of the list:

var d = document.createDocumentFragment();
d.appendChild(document.getElementsByTagName("LI")[0]);
d.childNodes[0].childNodes[0].nodeValue = "Milk";
document.getElementsByTagName("UL")[0].appendChild(d);

The result will be:

Before the changes:

  • Coffee
  • Tea

After the changes:

  • Tea
  • Milk
Try it Yourself »

Definition and Usage

The createDocumentFragment() method creates a imaginary Node object, with all the properties and methods of the Node object.

The createDocumentFragment() method is usefull when you want to extract parts of your document, change, add, or delete, some of the content, and insert it back to your document.

You can also use the document's Document object to perform these changes, but to prevent destroying the document structure, it can be safer to extract only parts of the document, make the changes, and insert the part back to the document.

Note: Nodes being appended to the document fragment, from the document, will be removed from the document.


Browser Support

Method
createDocumentFragment() Yes Yes Yes Yes Yes

Syntax

document.createDocumentFragment()

Parameters

None

Technical Details

Return Value: A DocumentFragment object, representing the created DocumentFragment node
DOM Version Core Level 1 Document Object

Document Object Reference Document Object