Created
December 11, 2017 23:42
-
-
Save javanigus/f041d4c92fb5b576512fd9402c0b8b62 to your computer and use it in GitHub Desktop.
Native JavaScript Detach and Attach an HTML Element
This file contains 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
// get the div | |
var div = document.querySelector('div.a'); | |
// detach the div | |
div = div.parentNode.removeChild(div); | |
// attach the div somewhere else | |
var footer = document.querySelector('.footer'); | |
footer.insertAdjacentHTML('afterend', div.outerHTML); | |
// listen to an event on the element and handle it | |
div.addEventListener("click", function (event) { | |
// do something | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment