Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the text of a DOM Text node?

Tags:

For a given Text node in the DOM, one can use one of these properties to retrieve its text:

  • textContent
  • data
  • nodeValue
  • wholeText

But which one to use? Which one is the most reliable and cross-browser supported?

(If multiple properties are 100% reliable and cross-browser, then which one would be most appropriate?)

like image 840
Šime Vidas Avatar asked Oct 07 '11 17:10

Šime Vidas


People also ask

How do I get text from an element?

Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants. If the element is empty, an empty string is returned.

What is a DOM text node?

domnodeelement. 10 Comments. The Document Object Model (DOM) is an interface that treats HTML or XML document as a tree structure, where each node is an object of the document. DOM also provides a set of methods to query the tree, alter the structure, style.


1 Answers

nodeValue should be is cross-browser compatible. It is part of the original DOM Level 2 specification.

Also have a look at the compatibility table of quirksmode.org for that matter (that's what I always use to see which properties are supported by which browsers).

like image 71
Felix Kling Avatar answered Sep 19 '22 13:09

Felix Kling