You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You probably should be using client-side templating instead of building DOM directly. If not, here is DOMBrew, a tiny dom builder:
# alias to a short variable$b= DOMBrew
build nodes
# build with css-like selector, (optional) text content, and attributesnode=$b"a#my-id.some-class", "hello world!", href:"/hello.html"# classes will be flattened and joined, data- will be hyphenized node=$b"#cont", class: ["cls1", "cls2"], data: { someProp:"hi", dat2:"hello" }
# use .text to build a text nodenode=$b.text('hello world!') # <- builds a text node# same asnode=$b('', 'hello world!') # <- builds a text node# you can wrap a dom element as $b node$bdocument.getElementById("my-elem") # <- 1 DOM node$b(nodes...or [nodes...]) # <- wrap multiple DOMBrew nodes (uses awesome DocumentFragment internally)
append / prepend children
# append / prepend children ($b nodes and / or dom elements) node.append(children...or [children...])
node.prepend(children...or [children...])
get the data
node.dom() # result as DOMnode.html() # result as html in a string
jQuery
# .$() to get as jquery node: node.$()$b::$=->$@dom()
$b.text('hello').$().wrap('<p>').appendTo(document.body)