Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Swaping two elements in plain JS
// Swaping two elements in plain JS
function swapElements(obj1, obj2) {
// create marker element and insert it where obj1 is
var temp = document.createElement("div");
obj1.parentNode.insertBefore(temp, obj1);
// move obj1 to right before obj2
obj2.parentNode.insertBefore(obj1, obj2);
// move obj2 to right before where obj1 used to be
temp.parentNode.insertBefore(obj2, temp);
// remove temporary marker node
// Swap it
swapElements(elm1, elm2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment