Rahul Desai
Rahul Desai

Reputation: 15501

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!

Upvotes: 3

Views: 6697

Answers (2)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44685

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.

Upvotes: 9

Darien
Darien

Reputation: 3592

If you want to insert text (not HTML) and don't want to worry about carefully escaping problem-characters like <, consider:

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

This works everywhere except IE8 and below, and unlike innerHtml it should be safe for problematic text like "a

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

You can see more about compatibility at this page.

In the console, the following error is thrown:

That error refers to network communication, which your code isn't doing. It's probably spurious, something particular to JSFiddle, especially considering the text jobofferinsidebar which sounds like an ad.

Upvotes: 1

Related Questions