Skip to content

Instantly share code, notes, and snippets.

@kerminz
Created April 28, 2019 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerminz/6415371ad482b95149bb70782320886f to your computer and use it in GitHub Desktop.
Save kerminz/6415371ad482b95149bb70782320886f to your computer and use it in GitHub Desktop.
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
temp.parentNode.removeChild(temp);
}
// Swap it
swapElements(elm1, elm2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment