Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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