Created
April 30, 2013 22:17
-
-
Save carlsednaoui/5492369 to your computer and use it in GitHub Desktop.
Without jQuery, it elegantly creates an element with attributes.
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 createElement(type, attrs) { | |
return Object.keys(attrs).reduce( | |
function(el, n) { el[n]=attrs[n]; return el; }, | |
document.createElement(type) | |
); | |
} | |
What does it do? Without jQuery, it elegantly creates an element with attributes. | |
createElement('a', { | |
href: 'http://google.com', | |
style: 'display: block;', | |
innerText: 'Google!' | |
}); | |
// HTMLElement | |
// <a href="http://google.com" style="display: block;">Google!</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment