HTML | DOM createDocumentFragment() Method
The createDocumentFragment() method in HTML DOM is used to create the document fragment which is useful to change the content of the document such as delete, modify or add a new node. This method creates the document fragment, then append the elements of the document to the document fragment and make the changes according to the need. Finally, append the document fragment to the document. It is a safe method and thus prevents destroying of the DOM structure.
Syntax:
document.createDocumentFragment()
Parameters: This method does not accepts any parameter.
Return Value: It returns the created DocumentFragment node.
Example:
<!DOCTYPE html> <html> <head> <!--script to delete first child of the list, and modify it --> <script> function myGeeks() { var doc = document.createDocumentFragment(); doc.appendChild (document.getElementsByTagName("li")[0]); doc.childNodes[0].childNodes[0].nodeValue = "SQL"; document.getElementsByTagName ("ul")[0].appendChild(doc); } </script> </head> <body> <h1>GeeksforGeeks<h1/> <h3> DOM createDocumentFragment() Method </h3> <p> Click on the button to change list element </p> <ul> <li>Data Structure</li> <li>Operating System</li> <li>C Programming</li> <li>DBMS</li> </ul> <button onclick = "myGeeks()"> Click Here! </button> </body> </html> |
Output:
Before click on the button:

After click on the button:

Supported Browsers: The browser supported by DOM createDocumentFragment() Method are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- HTML | DOM contains() Method
- HTML | DOM exitFullscreen() Method
- HTML | DOM removeAttributeNode() Method
- HTML | DOM importNode() Method
- HTML | DOM History go() Method
- HTML | DOM normalizeDocument() Method
- HTML | DOM hasChildNodes() Method
- HTML | DOM write() Method
- HTML | DOM cloneNode() Method
- HTML | method Attribute
- HTML | DOM console.log() Method
- HTML | DOM appendChild() Method
- HTML | DOM isEqualNode() Method
- HTML | DOM createTextNode() Method
- HTML | DOM fullscreenEnabled() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



