How to retrieve the text of a DOM Text node?

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

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 for that matter (that’s what I always use to see which properties are supported by which browsers).

When you’re 100% sure that it’s a text node you can use any of them.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   How to tell if two dates are in the same day or in the same hour? [duplicate]

Similar Posts