Boopathi Rajaa
Boopathi Rajaa

Reputation: 4729

Get dojo methods for a DOM element

I have 'this' pointing to a DOM element ( a div or a form ). I want to use dojo functions over that element. How will I do it.

Like in jQuery we do $(this).append() ....

is there anything like

dojo.foo(this).connect() 

or

dojo.connect(dojo.foo(this),"some", thing);

Upvotes: 4

Views: 2857

Answers (2)

Stephen Chung
Stephen Chung

Reputation: 14605

In Dojo, you are much closer to JavaScript (the raw metal) than in jQuery.

So in Dojo, you just do:

dojo.connect(this, ...);

You don't have to "wrap" the DOM element with a class object (like jQuery's $) to use the functionalities. A lot of functionalities in Dojo are not exposed as prototype properties of a class object, but as simple functions under the dojo.xxx namespace system.

For example (assume "this" points to a DOM node):

dojo.connect(this, "onclick", foo, "bar");   // Connects a handler to the Click event on the DOM node, with the context (i.e. this) set to the object foo
dojo.attr(this, "href", "http://www.hello.com");   // Sets an attribute on the DOM node
dojo.style(this, "display", "none");   // Sets the DOM node's style
dojo.addClass(this, "hello");   // Adds a class to the DOM node
alert(this.parentNode);   // You work the DOM nodes with raw JavaScript
dojo.empty(this);  // Empty all children in the DOM node
dojo.destroy(this);  // Destroy the DOM node and its children
dojo.place(this, someOtherNode);   // Put a DOM node as a child inside another node

Looping constructs:

dojo.forEach(array, ...);   // Instead of array.each(...) as in jQuery style

If you want to loop through a list of nodes, it actually looks like jQuery:

dojo.query('query string').filter(...).forEach(...);

Read the docs for more details.

Upvotes: 5

Boopathi Rajaa
Boopathi Rajaa

Reputation: 4729

I figured out a way that will work i guess. Don't know if it is the best solution..

Have to import NodeList-traverse to use functions like children, parent ... Reference: http://dojotoolkit.org/reference-guide/dojo/NodeList-traverse.html#dojo-nodelist-traverse

dojo.require("dojo.NodeList-traverse");

Reference: http://dojotoolkit.org/reference-guide/dojo/NodeList.html

var nl = new dojo.NodeList(this);

nl.parent();

Upvotes: 2

Related Questions