Skip to content

Instantly share code, notes, and snippets.

@thecooldaniel
Created November 8, 2012 20:11
Show Gist options
  • Save thecooldaniel/4041230 to your computer and use it in GitHub Desktop.
Save thecooldaniel/4041230 to your computer and use it in GitHub Desktop.
Creates a DOM object from a passed object following specific conventions. Uses light jQuery.
$(function(){
var ex = {
'tag':'audio',
'children': [
{
'tag':'source',
'att': [
{'name':'src','value':'../audio/beep.mp3'},
]
},
{
'tag':'source',
'att': [
{'name':'src','value':'../audio/beep.ogg'},
]
}
]
}
function domCreate(obj) {
var ref = document.createElement(obj.tag);
if(obj.att){for (var i = obj.att.length - 1; i >= 0; i--) {
$(ref).attr(obj.att[i].name, obj.att[i].value);
};}
if(obj.children){
for (var i = obj.children.length - 1; i >= 0; i--) {
ref.appendChild(domCreate(obj.children[i]));
};
}
console.log(ref);
return ref;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment