Skip to content

Instantly share code, notes, and snippets.

@edwardgalligan
Created July 6, 2016 16:09
Show Gist options
  • Save edwardgalligan/3e25c984cb904e46af8581bfe2a10544 to your computer and use it in GitHub Desktop.
Save edwardgalligan/3e25c984cb904e46af8581bfe2a10544 to your computer and use it in GitHub Desktop.
/**
* document.createElement re-implemented with a fluid interface
*
* Note: This will break lots of stuff. Don't use it.
* Note: This could be done by altering Element.prototype but that would break even more stuff
*
* Usage example:
* document.body.appendChild(
* document
* .createElement('a')
* .setAttribute('href', '#success')
* .appendChild(
* document.createTextNode('Testing...')
* )
* )
*/
document.createElement = (function() {
var createElement = document.createElement
return function(el) {
var node = createElement.call(document, el)
node.setAttribute = function(key, val) {
Element.prototype.setAttribute.call(node, key, val)
return node
}
node.appendChild = function(kid) {
Element.prototype.appendChild.call(node, kid)
return node
}
return node
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment