Skip to content

Instantly share code, notes, and snippets.

@komagata
Created December 13, 2008 12:51
Show Gist options
  • Save komagata/35459 to your computer and use it in GitHub Desktop.
Save komagata/35459 to your computer and use it in GitHub Desktop.
DOM building like MochiKit
function append(elem, value) {
if (typeof value == 'string') {
elem.innerHTML = value.replace(/\r\n/g, "\n").replace(/\n/g, '<br />')
} else if (typeof value == 'undefined') {
} else if (value.constructor == Array) {
for (var v in value) {arguments.callee(elem, value[v])}
} else {
elem.appendChild(value)
}
return elem
}
function tag(name, opts, value) {
var opts = opts || {}
var elem = document.createElement(name)
for (key in opts) {
if (key == 'class') {
elem['className'] = opts[key]
} else {
elem[key] = opts[key]
}
}
return append(elem, value)
}
var tags = ['div','span','a','h1','h2','h3','p','br','form','label','input','textarea']
for (var t in tags) {
eval("function "+tags[t]+"(opts, value){return tag('"+tags[t]+"', opts, value)}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment