Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 783
levik Avatar asked Mar 25 '09 06:03

levik


People also ask

How do I change the value of a dom?

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.

What is JavaScript TextNode?

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).

How do I get nodes in text?

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 .

Which property would you use to get the node that contains the content for an element?

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.


1 Answers

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.

like image 194
Ash Avatar answered Oct 06 '22 02:10

Ash