Search code examples
javascriptdojo

Set value/text of a <span> element using Dojo


I am developing a stopwatch application in an attempt to learn the Dojo Toolkit. So, to start with, I need to set the hours, minutes, seconds and milliseconds to 0.

I tried:

dojo.byId("hours").value = "00";

Also tried:

domAttr.set("hours", 00);

It didnt work. In the console, the following error is thrown:

GET http://jobs.jsfiddle.net/random.js?callback=Request.JSONP.request_map.request_0 500 (Internal Server Error) moo-clientcide-1.3.js?jobofferinsidebar:3146

Here is my fiddle so far.

Please help!


Solution

  • That's because value is only used when working with form fields. If you want to replace the actual content of the DOM node, you use innerHTML or textContent in stead. For example:

    dojo.byId("hours").innerHTML = "00";
    dojo.byId("hours").textContent = "00";
    

    or

    domAttr.set("hours", "innerHTML", "00");
    domAttr.set("hours", "textContent", "00");
    

    The difference between innerHTML and textContent is that the latter only allows text content (like the property says), while innerHTML also allows to input HTML. If you don't trust the input, you should definitely be using textContent.

    Be aware: you need to put quotes around the 00 because else it will be interpreted as a numeric value, which means the first 0 is skipped when you output it.

    I also changed your JSFiddle.