Search code examples
javascriptdomdhtml

Change textNode value


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 child nodes of their own.

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


Solution

  • If you have a specific text node and want to change its value you can use the nodeValue property:

    node.nodeValue = "new value";
    

    Note: innerText (and textContent) will return/set both the current node and all descendent nodes' text. Therefore, it may not be the behaviour you want.