Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript DOM tree duplicate for manipulation

Tags:

javascript

dom

Since the DOM tree of a page is active and always reflected in the browser, what is the best way to modify this DOM tree for some purpose without affecting the actual rendered tree ? Let's say my purpose is to swap certain children nodes and see how similar the DOM tree still remains.

Is creating a duplicate tree the only solution ? If it is, is there a function to do this ? Or do I need to write my own function to create a duplicate copy of the tree. I won't need all the attributes of the element object, so I can create a simpler object with a few attributes that point to the siblings and children.

like image 388
euphoria83 Avatar asked Nov 19 '10 07:11

euphoria83


1 Answers

You can use document.cloneNode(true), or the same method on another node. cloneNode clones any node, and the true means it should be recursive (deep). Obviously, this could have a significant performance cost on a large page.

like image 75
Matthew Flaschen Avatar answered Sep 20 '22 01:09

Matthew Flaschen