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.
Change the Value of an Attribute In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values. The way to change the value of an attribute, is to change its text value. This can be done using the setAttribute() method or setting the nodeValue property of the attribute node.
TextNode objects contain only text content without any HTML or XML markup. TextNode objects make it possible to insert texts into the document as nodes (appendChild, insertBefore).
To get the text node of an element with JavaScript, we can use the document. createNodeIterator method to iterate through all the text nodes. to select the p element with document. querySelector .
Use the textContent property to return the concatenation of the textContent of every child node. You can use it to set a text for a node.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With