tags:

views:

72

answers:

4

So I'm new to JavaScript and I'm trying to figure out why doesn't this work:

My function has this line:

document.getElementById("displayResult").value = ("test");

and this is my div:

<div id="displayResult"></div>
+1  A: 

You will want to use - .innerHTML no?

document.getElementById("displayResult").innerHTML = "<b>test</b>"; 
Kris Krause
For this little example, JQuery is not required. However, you should definitely check out JQuery, Prototype, etc.
Kris Krause
+1  A: 

.value is only a valid attribute on form fields. You likely want to use the following code:

document.getElementById("displayResult").innerHTML = "test";

Gdeglin
+8  A: 

div's don't have a value property. You want to set the .innerText property.

And by all means, have fun testing things yourself, but you'll find it a lot easier if you use a framework to do these things (like jQuery)

Noon Silk
Don't capitalise the I on innerText. Javascript is case sensitive, and generally functions and variables are written in camelCase.
Mark Withers
Cheers mark; typo on my part.
Noon Silk
A: 

you have to test if innerHTML is supported by your brwser. As it is not the DOM Standard. You can write it like

var oDiv = document.getElementById("displayResult")

if(typeof oDiv.innerHTML != undefined) {
      oDiv .innerHTML = message;
    } else {
      oDiv .appendChild(document.createTextNode(message));
}