Skip to content

Instantly share code, notes, and snippets.

@rmcvey
Created July 3, 2012 16:32
Show Gist options
  • Save rmcvey/3040852 to your computer and use it in GitHub Desktop.
Save rmcvey/3040852 to your computer and use it in GitHub Desktop.
DOM element factory
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