Skip to content

Instantly share code, notes, and snippets.

@dfurber
Created June 13, 2011 20:13
Show Gist options
  • Save dfurber/1023580 to your computer and use it in GitHub Desktop.
Save dfurber/1023580 to your computer and use it in GitHub Desktop.
CoffeeScript version of jQuery DOM Shortcut
do ->
setOptions = (el, attributes) ->
for own key, value of attributes
el.setAttribute key, value
el
_(['ol','ul','li','span','strong','p','h1','h2','h3','h4']).each (tag) ->
window["_#{tag}"] = (html, attributes, children...) ->
el = document.createElement tag
if attributes?
if attributes.jquery?
children.unshift attributes
else
el = setOptions el, attributes
if html?
if html.jquery?
children.unshift attributes
else
if typeof html is 'object'
el = setOptions el, html
else if typeof html is 'string'
el.innerHTML = html
el = $(el)
for child in children
el.append child
el
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment