Change textNode value

JavascriptDomDhtml

Javascript Problem Overview


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 answered below, content can be changed by setting the nodeValue property of these Objects.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

node.innerHTML="new value";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlevikView Question on Stackoverflow
Solution 1 - JavascriptAshView Answer on Stackoverflow
Solution 2 - JavascriptKdgDevView Answer on Stackoverflow