Modify textNode value
P粉805535434
P粉805535434 2023-10-17 20:55:25
0
2
643

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

I specifically wanted to see if I could change an existing node, rather than create a new node.

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

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

P粉805535434
P粉805535434

reply all(2)
P粉986937457

I believe innerHTML is used for this purpose... then again, this is not W3C approved... but it works...

node.innerHTML="new value";
P粉481035232

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

node.nodeValue="new value";

Notice:

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template