Posted By

jatkins on 03/06/11


replace DOM swap childNode

Versions (?)

Swap child nodes

 / Published in: JavaScript

Released into the public domain. Update 7/20/2011: Rewritten with a method that shouldn't lose values/selected files/etc. Earlier versions based on a solution found on Stack Overflow.

  1. function swapNodes(node1, node2) {
  2. elementAfterFirstNode = nextElement(node1);
  3. elementAfterSecondNode = nextElement(node2);
  4. node1.parentNode.insertBefore(node1, elementAfterSecondNode);
  5. node2.parentNode.insertBefore(node2, elementAfterFirstNode);
  6. }

Report this snippet  

You need to login to post a comment.