dom-build
v1.1.1
Published
Build DOM trees in Javascript with shortcut syntax for grabbing references to generated elements
Downloads
45
Readme
dom-build
Build DOM trees in Javascript with shortcut syntax for grabbing references to generated elements.
There are lots of libraries that perform a similar function but none I've encountered allow you to get a reference to anything but the root node. dom-build
introduces the !key
selector syntax to indicate that a generated element should be attached to the returned object structure via the named key. Example:
var d = require('dom-build');
var ui = d('#root.a.b.c!foo',
"This is a text node", d('br'),
"This is another text node", d('br'),
d('span.myMessage',
d('%text!myMessage',
'This is an explicit text node; it will be returned.',
' Multiple strings ',
'can be added'
)
),
d('br'),
d('a.active!link',
{ href: "/foo/bar",
onclick: function(evt) { evt.preventDefault(); alert("hello!"); } },
"Click me! ", [
d("b!bold", "here's some bold text"),
" ",
d("i", "here's some italic text")
]
),
d("div", {style: {width: 100, height: 100, backgroundColor: 'red'}})
);
This yields the ui
object comprising these keys:
root
: the element created by the outermost call tod()
myMessage
: a raw text node; the contents ofspan.myMessage
foo
: equivalent toroot
;div#root.a.b.c
link
:a.active
bold
:<b>here's some bold text</b>