Created
February 13, 2013 07:11
-
-
Save rainyjune/4942821 to your computer and use it in GitHub Desktop.
DOM Manipulation: append, prepend, replaceWith, remove
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
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