Created
July 3, 2012 16:32
-
-
Save rmcvey/3040852 to your computer and use it in GitHub Desktop.
DOM element factory
This file contains 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
var element = { | |
create: function(tag_type, attributes){ | |
var el = document.createElement(tag_type) | |
, styles = attributes['style'] || [] | |
, i | |
, x | |
, style_string = ''; | |
delete attributes['style']; | |
for(i in attributes){ | |
el.setAttribute(i, attributes[i]); | |
} | |
for(x in styles){ | |
style_string += x+':'+styles[x]+';'; | |
} | |
el.setAttribute('style', style_string); | |
return el; | |
} | |
} | |
element.create('div', { | |
id: 'foo', | |
class: 'bar', | |
style: { | |
'position': 'absolute', | |
'top': 0, | |
'left': 0, | |
'background-color': '#000' | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment