Skip to content

Instantly share code, notes, and snippets.

@rainyjune
Created February 13, 2013 07:11
Show Gist options
  • Save rainyjune/4942821 to your computer and use it in GitHub Desktop.
Save rainyjune/4942821 to your computer and use it in GitHub Desktop.
DOM Manipulation: append, prepend, replaceWith, remove
function append(content, element) {
var element = element || document.body;
element.appendChild(content);
}
function prepend(content, element) {
var element = element || document.body;
var firstNode = element.firstChild;
element.insertBefore(content, firstNode);
}
function replaceWith(newNode, oldNode) {
var parent = oldNode.parentNode;
parent.replaceChild(newNode, oldNode);
}
function remove(node) {
node.parentNode.removeChild(node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment