HTML DOM createDocumentFragment() Method

## JavaScript Reference

Overview

## JavaScript Objects

JS Array JS Boolean JS Date JS Math JS Number JS String JS RegExp JS Global

## Browser Objects

Window Navigator Screen History Location

## HTML DOM Objects

DOM Document DOM Elements DOM Attributes DOM Events

## HTML Objects

DOM Anchor DOM Area DOM Base DOM Body DOM Button DOM Form DOM Frame/IFrame DOM Frameset DOM Image DOM Input Button DOM Input Checkbox DOM Input File DOM Input Hidden DOM Input Password DOM Input Radio DOM Input Reset DOM Input Submit DOM Input Text DOM Link DOM Meta DOM Object DOM Option DOM Select DOM Style DOM Table DOM td / th DOM tr DOM Textarea

# HTML DOM createDocumentFragment() Method

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 extraxt 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

The createDocumentFragment() method is supported in all major browsers.

## Syntax

document.createDocumentFragment()

## Parameters

None.

## Return Value

Type | Description
--- | ---
DocumentFragment object | The created DocumentFragment node

## Technical Details

DOM Version | Core Level 1 Document Object

Document Object

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE