Take the 2-minute tour ×

I'm just getting started with dojo, and I've understood that dojo.query is the same as $ in jQuery.

But I haven't figured out what it returns. Is it a specialized object like in jQuery?

What I'm trying to do (with no luck) is:

dojo.query("output").innerHTML = data;
//this doesn't work either:
//tried accessing by id as well
//and tried to access a div, incase dojo has some issues with html5 elements

And I'm currently using the new html5 elements:

<output id="output">Output goes here</output>
<div id="divOutput">non-html5 output goes here</div>

And I can't seem to find a good list on what to do with objects returned by dojo.query()..

edit: Okay, I think dojo is just messing with me now. I found this method: addContent() and that works on the above selector. But I don't want to add content, I want to replace content...

share|improve this question
add comment

6 Answers

up vote 3 down vote accepted

The query method returns a NodeList object.

In the ref for NodeList you can find a list of functions that you can apply to the list of elements. There is no innerHTML function for the list, but the html function should work.

There is no "output" element in HTML, perhaps you try to target elements with the class name "output"?


Or the element with id "output"?

share|improve this answer
Nope, I'm using the HTML5 element output, but just to be sure I've tried adding an id to the element, and doing the same...still no result. –  peirix Oct 9 '09 at 9:01
@peirix: I see. To use the output element you should set it's value attribute, not it's innerHTML property. –  Guffa Oct 9 '09 at 9:19
The output element works like a div. It's just a content holder, so it doesn't have a value attribute. w3schools.com/tags/html5_output.asp But I still can't get the html() method to work, even on divs... –  peirix Oct 9 '09 at 9:42
Sheez! You were right, but you forgot to mention the dojo.require. For the html() method to work you need to add dojo.require("dojo.NodeList-html"); in your script... –  peirix Oct 9 '09 at 10:21
Oh, it's in a separate file? The documentation doesn't mention anything about that on the hmtl function... I've never used dojo myself, so I don't know how the code is divided into files... –  Guffa Oct 9 '09 at 10:36
add comment

If you want to replace all the output tags' content with the same thing, then this code should always work:

// replace the contents of ALL <output> tags
dojo.query('output').forEach(function(node) { node.innerHTML = data; });

Dojo also provides a little shortcut for these kinds of things. You can specify a string to NodeList's forEach function like this:

// replace the contents of ALL <output> tags (as long as data is global)
dojo.query('output').forEach("item.innerHTML = data;");

The word item in the string is special. (This is a pain to debug, so it might not be worth it.)

share|improve this answer
add comment

As was said above, query method returns NodeList object, so you can iterate it's result as array, or use dojo methods that work with NodeList (e.g. attr):

dojo.query("#divOutput").attr("innerHTML", data);

But as soon as you are trying to query nodes by id, it would be better to use dojo.byId() method, which returns domNode:

dojo.byId("divOutput").innerHTML = data;

Or in more dojo style:

dojo.attr(dojo.byId("divOutput"), "innerHTML", data)
share|improve this answer
add comment

Try this by adding the [0] like this:

dojo.query("output")[0].innerHTML = data;
share|improve this answer
add comment

Also, there is a dojox.jq wrapper (in development, coming in 1.4) which emulates the JQuery return object APIs

share|improve this answer
add comment

The documentation seems to be a mess, this is the only thing i get to work with 1.7,

dojo.query("whatever").forEach(function(node, index, array)
share|improve this answer
add comment

Your Answer


