+ 2

How to get Text content of an Element only not it's child also ?

When we uses textContent method on an Element node we get text from all it's children and from itself also But want to avoid text from it's children I hope u guys help me https://code.sololearn.com/WT4WgI8RDDDs/?ref=app

9th Oct 2020, 1:09 AM
Abhay
Abhay - avatar
1 Answer
+ 2
I think the only way to do it is to iterate through all the children and filter out the text nodes by hand... something like [...node.children].filter(n => n.nodeType === Node.TEXT_NODE).map(n => n.textContent).join(""); (untested) Anyhow to me this indicates that you should probably wrap your text in a <span> and then you can just grab the .textContent off that. Gives you more control too.
9th Oct 2020, 2:00 AM
Schindlabua
Schindlabua - avatar