What is the easiest way to swap the order of child nodes?
For example I want childNode to be childNode and vice-versa.
There is no need for cloning. You can just move one node before the other. The
.insertBefore() method will take it from its current location and insert it somewhere else (thus moving it):
You get the parent of the node. You then call the insertBefore method on the parent and you pass it the childNode node and tell it you want it inserted before childNode. That will give you the result of swapping their order so 4 will be before 3 when it’s done.
Reference documentation on insertBefore.
Any node that is inserted into the DOM that is already in the DOM is first removed automatically and then inserted back so there is no need to manually remove it first.