Document createDocumentFragment() Method - Javascript DOM

Javascript examples for DOM:Document createDocumentFragment

Description

The createDocumentFragment() method creates a Node object.

Parameters

None

Return Value:

A DocumentFragment object, representing the created DocumentFragment node

The following code shows how to Create a documentFragment node and append a child to a list item.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<ul><li>A</li><li>B</li></ul>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from  w  ww .j  av a  2s . c  om*/
    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>

Related Tutorials