Reputation: 53323
When I need to change a text within a span element, which one should I use and what is the difference:
var spnDetailDisplay=document.getElementById('spnDetailDisplay');
spnDetailDisplay.innerText=sDetail;
or
var spnDetailDisplay=document.getElementById('spnDetailDisplay');
spnDetailDisplay.childNodes[0].nodeValue=sDetail;
Upvotes: 24
Views: 11405
Reputation: 86
<script>
and <style>
.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
Upvotes: 4
Reputation: 118781
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
.
Upvotes: 18