Created
November 8, 2012 20:11
-
-
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.
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
$(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