X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

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

Internet Explorer Firefox Opera Google Chrome Safari

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


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook