How to use DOM Object CreateDocumentFragment in JavaScript

In this article I am going to explain about DOM object CreateDocumentFragment() Method in JavaScript.
  • 1753

JavaScript DOM Object CreatDocumentFragment() Method

JavaScript JavaSCript CreateDocumentFragment() method use for create empty DocumentFragment object. CreateDocumetFragment() method is very use full when we want to extract part of document modify some of content and insert it to back of document.

Syntax

Document.CreateDocumentFragment()

Example

<html>

<head>

    <ul>

        <li>Employee</li><li>Manager</li><li>person</li></ul>

    <script type="text/javascript">

 

        function myfun() {

            var mm = document.createDocumentFragment();

            mm.appendChild(document.getElementsByTagName("LI")[0]);

            mm.childNodes[0].childNodes[0].nodeValue = "Worker";

            document.getElementsByTagName("UL")[0].appendChild(mm);

        };

 

    </script>

</head>

<body>

    <p>

        click button for change list of item</p>

    <button onclick="myfun()">

        click me</button>

</body>

</html>

 

Output

 

Before Click button


frag1.gif

 

After click button


frag2.gif

 

You may also want to read these related articles: here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.