How do I choose between innerText or nodeValue?
- nodeValue is standard recommendation W3C.
- innerText is Internet Explorer specific, not the standard. And:
- Does not return content of
<script>
and<style>
. - Style awareness, does not return text of hidden elements.
- CSS styling awareness, it will trigger a reflow.
- Does not return content of
However in versions of all major browsers as of March of 2016 innerText is supported.
In general, they are similar, but if you need the best performance, nodeValue should be used.
Link for performance test: https://jsperf.com/read-innerhtml-vs-innertext-vs-nodevalue-vs-textcontent/13
For elements with text content, they're the same. See this MDC article for information on nodeValue
.
From this article:
If the element has no sub-elements, just text, then it (normally) has one child node, accessed as
ElemRef.childNodes[0]
. In such precise case, the W3C web standards equivalent ofElemRef.innerText
isElemRef.childNodes[0].nodeValue
.