I inherited some old Javascript the other day that looks something like this:

document.getElementById('msg').firstChild.nodeValue = message;

Inside the document there's a span tag that this writes to:

<span id="msg"></span>

Why use this arcane firstChild.nodeValue when you can use innerHTML??:

document.getElementById('msg').innerHTML = message;

It was not until I made this fix that the Javascript started to work in my Firefox. How could that ever have worked? All Javascript gurus out there, what am I missing?

djo - 10 September 2005 [«« Reply to this]
http://www.developer-x.com/content/innerhtml/default.html

Scroll down to "Arguments for and against" for the precis. But it appears to boil down to "DOM is hard to use and powerful. InnerHTML is easy to use, widely implemented but non-standard. Your call."
Peter Bengtsson - 13 September 2005 [«« Reply to this]
thanks djo. I feel compelled to go back to firstChild.nodeValue but innerHTML seems a much better solution (even though it's "wrong") because I like things simple.
Joerg - 17 October 2005 [«« Reply to this]
check this:

<html>
<head>
<script type="text/javascript">
function initial(){
document.getElementById("msg").innerHTML = "<div style='margin: 10px; padding: 20px;'><i>Text</i></div>";
document.getElementById("msg1").firstChild.nodeValue = "<div style='margin: 10px; padding: 20px;'><i>Text</i></div>";
}
</script>

</head>

<body onload="initial()">
<div id="msg">empty</div>
<div id="msg1">empty1</div>

<br><br><br><br>
<a target="firefox" href="http://validator.w3.org/check?uri=referer">checkme with validator...!</a>
</body>
</html>
Peter Bengtsson - 18 October 2005 [«« Reply to this]
I see! It doesn't work.
http://www.peterbe.com/plog/firstchild.nodevalue-vs.-innerhtml/test.html
George - 11 July 2006 [«« Reply to this]
The answer to your initial question about how the .firstChild.nodeValue code could have ever worked with the span tag you give is that it will work in Mozilla Firefox (tested on v1.5) if there is a space inside the span tag:

&lt;span id="msg"&gt;(space here)&lt;/span&gt;

...otherwise Mozilla's JavaScript Console gives the error:

document.getElementById("msg").firstChild has no properties

Unfortunately, adding just a space still gives an error in IE 6 so the true answer is that &nbsp; needs to be added. Maybe the old code was cleaned up and some spaces or &nbsp;'s were removed? Without something inside the span tag, there's no text node so .firstChild.nodeValue doesn't exist.

PS I hope you don't mind me posting to something this old, but this page places very high for a Google search on ".firstChild.nodeValue" so I thought some more comments might help anyone else who stumbles across it.
thanuja - 23 April 2007 [«« Reply to this]
hi...

someone help me how to pass anchor text to next page using session
Brazilian ICT Offshore - 14 September 2008 [«« Reply to this]
x = document.createTextNode('test');
document.getElementById('msg').appendChild(x);

Works both in FF and IE.

Example:
http://www.haan.net/test/demo_createTextNode.php

Cheers,
Brazilian ICT Offshore
www.haan.net
PaulnOZ - 19 August 2009 [«« Reply to this]
Unfortunately, innerHTML, textContent & firstChild.nodeValue don't preserve any extra white spaces in the same way that innerText does. I'm still scratch'n, so if anyone knows a way I'm all ears.
Samuel Komfi - 19 October 2009 [«« Reply to this]
val = msg.firstChild;
actualValue = val.data;
Anonymous - 30 August 2010 [«« Reply to this]
using innerHtml can cause errors in some older browsers because it's not offical.
Anonymous - 20 February 2011 [«« Reply to this]
screw older browsers.
dave - 23 May 2011 [«« Reply to this]
Good question,

For a javascript beginner like myself which would you recommend for writing form fields on the same page?

Inner HTML or FirstChild?

Thanks

Dave
www.mymoneycalculator.com.au
Chuck Kollars - 14 November 2013 [«« Reply to this]
There can also be a fairly large performance difference. Performance of the .firstNode.nodeValue method can be several times faster than the .innerHTML method, because assignment to firstNode.nodeValue makes no attempt to parse any potential HTML or make any DOM changes. (In fact the .firstNode.nodeValue method doesn't even notice embedded HTML, and so will misbehave if given anything more than just a simple text string).

Whether or not the performance of just this one operation significantly impacts the overall performance of the webpage, and whether or not performance is an issue at all, and whether or not this tradeoff of performance for clarity is considered a good deal, are all of course different for every situation. About all one can say is YMMV.
KEVIN MA - 20 April 2014 [«« Reply to this]
how come we only get the line <div style='margin: 10px; padding: 20px;'><i>Text</i></div> for document.getElementById("msg1").firstChild.nodeValue

but not

document.getElementById("msg").innerHTML


Your email will never ever be published