Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Dojo 1.6 equivalent of jQuery(html)?

Tags:

jquery

dom

dojo

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.

like image 860
Nikki Erwin Ramirez Avatar asked Feb 22 '23 19:02

Nikki Erwin Ramirez


2 Answers

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

like image 118
Frode Avatar answered Mar 03 '23 00:03

Frode


That would be the create method.

like image 41
Darin Dimitrov Avatar answered Mar 02 '23 23:03

Darin Dimitrov