Javascript Reference - HTML DOM createDocumentFragment() Method








The createDocumentFragment() method creates a Node object.

Browser Support

createDocumentFragment Yes Yes Yes Yes Yes

Syntax

document.createDocumentFragment()

Parameters

None.

Return Value

A DocumentFragment object

Example

The following code shows how to create a documentFragment node and append a child to it.


<!DOCTYPE html>
<html>
<body>
<ul><li>A</li><li>B</li></ul>
<button onclick="myFunction()">test</button>
<script>
function myFunction()<!--from   www  .java 2s  .co  m-->
{
    var d=document.createDocumentFragment();
    d.appendChild(document.getElementsByTagName("LI")[0]);
    d.childNodes[0].childNodes[0].nodeValue="new";
    document.getElementsByTagName("UL")[0].appendChild(d);
}
</script>

</body>
</html>

The code above is rendered as follows: