Getting Started with Dojo

September 26, 2011

Here's some very basic getting started instructions for the Dojo JavaScript toolkit. Unlike jQuery, there's generally more raw JavaScript involved.

Google CDN

The basic library can be installed from Google's CDN:

<script type="text/javascript" src=""></script>

Running Code on Page Load

To run JavaScript on page load, pass a javascript function into dojo.ready:

function init() {
    dojo.byId("greeting").innerHTML += ", from " + dojo.version;


Dojo loads a minimal amount of the library up front, other modules can be loaded using a dojo.require call. Here's an example that loads the dojo.fx library and then using it to slide a html element to a new position


function init() {
    dojo.byId("greeting").innerHTML += ", from " + dojo.version;

       top: 100,
       left: 200,
       node: dojo.byId("greeting")

Creating Nodes

Dojo provides the dojo.create function to support creating dom nodes. The first parameter is the tag name and the second is an object with the attributes you want to create on the node. The third element is optional, and is the parent to attach to.

var list = dojo.byId("list");

dojo.create("li", {
    innerHTML: "Six"
}, list);

dojo.create("li", {
    innerHTML: "Seven",
    className: "seven",
    style: {
        fontWeight: "bold"
}, list);

You can also specify another parameter that controls the relationship between the parent node to add to, and the new node. This defaults to "last":

var three = dojo.byId("three");

dojo.create("li", {
    innerHTML: "Three and a half"
}, three, "after");

Valid values for the last parameter are: "before", "after", "replace", "only", "first", or "last". See for more details.

Finding Nodes

Dojo uses dojo.query to peform jQuery like searching for nodes based on a CSS selector. This always returns an array (actually a dojo NodeList instance):

var odds = dojo.query(".odd");

NodeList has supporting methods, so to iterate around all the returned nodes:

dojo.query(".odd").forEach(function(node, index, nodelist){
    dojo.addClass(node, "red");

There are convenience methods on NodeList for many DOM functions, so this code can be simplified as:


This will return the NodeList, so you can chain just like with jQuery:



Dijit is the Dojo presentation layer framework, complete with themeing. You can get the CSS for that (here using the Tundra theme) from Google's CDN:

<link rel="stylesheet" href="">

Remember to add the theme name as a class on the body tag:

<body class="tundra">

Here's we're going to load the digit modules for a button and a dialog box, and show a dialog when the button is clicked. First, include CSS, then load the appropriate modules:


Next, create a button in the HTML and attach a JavaScript onClick handler to it:

<button dojoType="dijit.form.Button" id="buttonTest">
    Click me
    <script type="dojo/method" event="onClick">

Finally, we need to define the dialog contents:

<div dojoType="dijit.Dialog" id="dialogTest" title="Clicky-click!">You clicked and now you see this box.</div>

Alternatively, we can create the dialog in code, rather than declare a separate div in the HTML.

Create the following function on the page:

function dialogAlert(pTitle, pContent) {
    var thisdialog = new dijit.Dialog({ title: pTitle, content: pContent });

and then have the button call this function when clicked:

<script type="dojo/method" event="onClick">
    dialogAlert('Title', 'You clicked and now you see this box.');


Tags: javascript dojo