Skip to content

Instantly share code, notes, and snippets.

@carlsednaoui
Created April 30, 2013 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlsednaoui/5492369 to your computer and use it in GitHub Desktop.
Save carlsednaoui/5492369 to your computer and use it in GitHub Desktop.
Without jQuery, it elegantly creates an element with attributes.
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