Created
April 28, 2019 21:25
-
-
Save kerminz/6415371ad482b95149bb70782320886f to your computer and use it in GitHub Desktop.
Swaping two elements in plain JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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