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