In jQuery, you can easily create DOM nodes from raw HTML. This is especially useful when using templates.
What is the equivalent in Dojo?
(FYI: I'm migrating something from jQuery to Dojo. The raw HTML is generated from Underscore.js templates, and I'd like to avoid throwing them away.)
[UPDATE: 2012-01-19 7:17pm GMT+8] As per @esailija 's comment, dojo.toDom is indeed the equivalent of jQuery(html) -- unfortunately, it's only been added to Dojo 1.7, while I'm kinda stuck with Dojo 1.6. Updated the question to reflect the version.
I think perhaps what you are looking for is a combination of dojo.place
and dojo._toDom
(available without the underscore in >=1.7).
The toDom
function takes the string and makes it into a DOM element or document fragment.
n = dojo._toDom("<li>foo</li>"); // n is a single DOM node
n = dojo._toDom("foo"); // n is a DOM text node
n = dojo._toDom("<li>foo</li><li>bar</li>"); // n is a DOM document fragment
The place
function also takes a string and a target.
dojo.place("<li>foo</li>", dojo.byId("baz")); // li element is added to
// element with id "baz"
dojo.place("<li>foo</li>", "baz"); // Same as above.
dojo.place("foo", "baz"); // Note: Element with id "foo"
// is placed in element with
// id "baz"
dojo.place(dojo._toDom("foo"), "baz"); // Text node "foo" is placed
// in element with id "baz"
Notice the third example: if the string doesn't start with a <
, it's treated as the id of an element somewhere else in the document.
Btw, the dojo.place
function also takes a third position argument, which can be "first", "last", "replace", "before", "after" (and probably a few other things).
http://dojotoolkit.org/reference-guide/dojo/place.html
That would be the create method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With