views:

809

answers:

2

Is there any way to change the value of a DOM textNode in web browser?

I specifically want to see if I can change the existing node, rather than creating a new one.

To clarify, I need to do this with Javascript. All text in the browser is stored in #textNodes which are children of other HTML nodes, but cannot have childr nodes of their own.

As answered below, content can be changed by setting the nodeValue property of these Objects.

A: 

I believe innerHTML is used for this... Then again, that isn't W3C approved... but it works...

WebDevHobo
+4  A: 

If you have a specific node (of type #text) and want to change its value you can use the nodeValue property:

node.nodeValue="new value";

Note:

innerText (and possibly textContent) will return/set both the current node and all descendent nodes text, and so may not be the behaviour you want/expect.

Ash
Yes, nodeValue is perfect for this. innerText and textContent differ in this regard: IE (who created innerText) doesn't support it on #text nodes (since there are no child nodes to textify), whereas textContent was meant to be used on both #Element AND #text nodes.
Crescent Fresh
There's also ‘data’ as a short synonym for ‘nodeValue’ on Text (as well as CDATASection and Comment).
bobince
nodeValue is the one I needed. For some reason I thought it was read-only, but seems to work on FF3 and IE7. Thanks!
levik