Skip to content

Instantly share code, notes, and snippets.

@javanigus
Created December 11, 2017 23:42
Show Gist options
  • Save javanigus/f041d4c92fb5b576512fd9402c0b8b62 to your computer and use it in GitHub Desktop.
Save javanigus/f041d4c92fb5b576512fd9402c0b8b62 to your computer and use it in GitHub Desktop.
Native JavaScript Detach and Attach an HTML Element
// 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