
Dojo is a fantastic toolkit that we have used on many projects here at moresoda. Although we still love and use jQuery nearly everyday, we use Dojo on projects where the front end requirements are more complicated than your average DOM manipulation and HTML5 shims.
This article isnt about preaching Dojos benefits though. If you have a read of the features and benefits of Dojo you can make up your own mind. That being said, Dojo can be harder to get into since is it a much larger than jQuery.
Hence my aim here is to provide a simple, unbiased side by side comparison of common jQuery operations and how they are achieved in Dojo.
Lets go!
Loading the script from the Google CDN
Both can be loaded from the Google CDN. If you use any components of Dojo that are not part of Dojo Base (the file being loaded by the script tag below), they will be dynamically pulled from the CDN as well.
//jQuery <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> //Dojo <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7/dojo/dojo.js" type="text/javascript"></script>
Waiting for the DOM Ready Event
The Dojo ready and jQuery ready methods are identical. With Dojo you dont need to specify the document, as the current document is implied.
//jQuery
$(document).ready( function(){
// Your code here
});
//Dojo
dojo.ready( function () {
// Your code here
})
Querying the DOM
Both Dojo and jQuery support the CSS3 selectors. Hence there is no real difference in usage here apart from dojo being more verbose.
When querying the DOM, each method returns their own array type collection of nodes that have been matched. jQuery returns a jQuery Collection and Dojo returns a Dojo NodeList. Both can be accessed just like an array but they allow chaining of methods to quickly add/remove classes, change properties etc.
//jQuery
$("some CSS3 selector"); // Returns a jQuery Collection (kind of like an array)
//Dojo
dojo.query("some CSS3 selector"); // Returns a Dojo Node List (kind of like an array)
Adding and removing classes
Both methods are identical and can be chained infinitely to add and remove classes in a single call.
Adding
//jQuery
$("p:last-of-type").addClass("last");
//Dojo
dojo.query("p:last-of-type").addClass("last");
Removing
//jQuery
$("p:last-of-type").removeClass("last");
//Dojo
dojo.query("p:last-of-type").removeClass("last");
Getting and Manipulating HTML Attributes
Once again the setter and getter methods are identical, making it very easy to remember! As with class manipulation, both will allow these to be chained together for convenience.
Getting attributes
//jQuery
$("a.external_link").attr("target");
//Dojo
dojo.query("a.external_link").attr("target");
Setting attributes
//jQuery
$("a.external_link").attr("target", "_blank");
//Dojo
dojo.query("a.external_link").attr("target", "_blank");
Looping over multiple queried DOM nodes
Since the object returned from querying the DOM is array like in nature, you can loop through the items. Both jQuery Collections and Dojo NodeLists contain built in methods that allow you to conveniently lopp through the items.
Take note that the technique for accessing the current item in the loop is different:
//jQuery
$(".someClass").each( function() {
$(this).addClass("active"); //Current node in the loop is available as "this"
});
//Dojo
dojo.query(".someClass").forEach( function(node) {
dojo.addClass(node, "active"); //Current node in the loop is available as function argument "node"
});
Creating HTML Nodes
jQuery introduced a novel way of creating HTML elements which was by parsing a string of HTML text string into its correct HTML Object. Due to its convenience, Dojo has a method that allows you to achieve the same thing.
//jQuery - HTML Parsing
var newNode = $("<h1>This is a new heading</h1>");
//jQuery - Alternate
var newNode = $("<h1/>").prop({
"innerHTML" : "This is a new heading"
});
//Dojo - HTML parsing
var newNode = dojo.toDom("<h1>This is a new heading</h1>");
//Dojo - Alternate
var newNode = dojo.create("h1", {
"innerHTML" : "This is a new heading"
})
Inserting HTML nodes into the DOM
As opposed to using multiple methods to dictate the position to insert a new element, Dojo opts for a single method with a string argument that dictates the position the new node should be inserted (eg: first, last, after etc).
Append
//jQuery
$("#header").append(newNode);
//Dojo
dojo.place( newNode, "header", "last")
Prepend
//jQuery
$("#header").prepend(newNode);
//Dojo
dojo.place( newNode, "header", "first");
Before
//jQuery
$("#header").before(newNode);
//Dojo
dojo.place( newNode, "header", "before");
After
//jQuery
$("#header").after(newNode);
//Dojo
dojo.place( newNode, "header", "after");
Creating and Inserting HTML nodes in a single go
Both provide convenience methods for creating and inserting in a single call. Dojo’s create method’s third parameter expects the name of an element ID (in the example below, “#article”) but it will also accept an HTML node found by using the dojo.query method.
//jQuery
$("<p>Howdy Ho!</p>").prependTo("#article");
//Dojo
dojo.create("p", {
"innerHTML": "Howdy Ho!"
}, "article", "first"); // Third parameter to this function accepts a CSS ID or an HTML Node.
Removing HTML Nodes from the page
Neither will actively destroy the object, but merely detach it from the DOM. If the node removed is not referenced anywhere else in the currently running script, then the browser’s garbage collection will destroy the object for you. Dojo does have an additional dojo.destroy method which will destroy an object for you.
//jQuery
$("#header li").remove(); //Removes all items in the collection from the page
//Dojo
dojo.query("#header li").orphan(); //Loops over each item found and hands it over to the destroy node method
Traversing
Dojo does not load the traversing methods by default. Hence if you want to use the methods below you will need to load the NodeList-traverse extensions before using them.
Next element
//jQuery
$("li:first-of-type").next(); //Next nodes after found nodes, if available
$("li:first-of-type").next(".active"); //Next nodes, but only if they match the CSS selector
//Dojo
dojo.require("dojo.NodeList-traverse"); //This loads the traversion extensions to Dojo NodeLists. Only needs to be called once
dojo.query("li:first-of-type").next();
dojo.query("li:first-of-type").next(".active");
Previous element
//jQuery
$("li:first-of-type").prev(); //Previous nodes after found nodes, if available
$("li:first-of-type").prev(".active"); //Previous nodes, but only if they match the CSS selector
//Dojo
dojo.require("dojo.NodeList-traverse");
dojo.query("li:last-of-type").prev();
dojo.query("li:last-of-type").prev(".active");
Child elements
//jQuery
$("ul.nav").children(); // All child nodes of the found nodes
$("ul.nav").children(".active"); // All child nodes, but only if they match the CSS selector
//Dojo
dojo.require("dojo.NodeList-traverse");
dojo.query("ul.nav").children();
dojo.query("ul.nav").children(".active");
Parent element
// jQuery
$("li.active").parent();
// Dojo
dojo.require("dojo.NodeList-traverse");
dojo.query("ul.nav").parent();


Comments
Samuele C.
February 9th, 2012
Great! Usefull!!
Chris Mitchell
February 9th, 2012
Nice post, since you’re using Dojo 1.7.x from CDN, you may want to consider updating the article to use the simpler (and lighter-weight) AMD-style api’s for Dojo, rather than dojo.XXX api’s (see the latest Dojo tutorials at dojotoolkit.org (All the previous tutorials have recently been re-written to follow the new AMD-style introduced in 1.7)
Chris Mitchell
February 9th, 2012
Also, your readers may want to know that it’s now simple to use Dojo 1.7 and JQuery 1.7 *together*, see this recent article by Christophe Jolif for how this is achieved: http://ibm.co/sCXYWy
Christopher Imrie
February 9th, 2012
Thanks for the kind words Chris.
The new AMD features in Dojo really are awesome and I’ll touch on them at the end of this series of articles.
I purposefully used the dojo.xxx style API in this (and subsequent) articles to highlight how similar the API’s are. I wanted the barrier for entry to be as low as possible: that of a beginner jQuery user.
I think that if you can relate or explain a new library to someone using techniques that they are familiar with then it becomes easier to learn.
Eddie Monge
February 11th, 2012
You don’t need to specify document for jQuery either for the ready block:
$(function() { }):
is the same thing as
$(document).ready(function() { });
The section “Looping over multiple queried DOM nodes” is using a pretty bad example since you wouldn’t do a .each() to add a class in jQuery. .addClass() already does an internal .each(). Dojo is probably the same.
jQuery also supports the dojo create method in “Creating HTML Nodes”:
var newNode = $(”<h1>”, {
text : “This is a new heading”
});
ben hockey
February 13th, 2012
dojo.place can create and insert in one step:
dojo.place(‘Howdy Ho!’, ‘article’, ‘first’);
Ben Hockey
February 13th, 2012
aargh… formatting got eaten for my previous comment. anyhow, you should get the idea based on your example.
Christopher Imrie
February 13th, 2012
@Ben Thanks for the tip!
There you, go! I learn something new about Dojo every day!
Akhil
February 23rd, 2012
I am having a doubt in dojo.query. i have a dom structure and i am trying to select a specific class by using
dojo.query(className) , but its not returning the nodelist in the same order as its in dom.
Is there any way I can get in the same order?
Commenting is not available in this channel entry.