That's very close, except I believe it's more like this (according to
the W3C and TraceMonkey):

var newTextStr = String(newText)
while (el.firstChild) { el.removeChild(el.firstChild); }
if(newText !== null && newTextStr !== "")
  el.appendChild(el.ownerDocument.createTextNode(newTextStr));

since String(null) returns "null", but should not append a TextNode
containing the String "null". For all other falsey values, their
String representations should be appended.

On Jun 17, 7:19 pm, Mike Samuel <[email protected]> wrote:
> 2010/6/17 Michael Ficarra <[email protected]>:
>
> > I believe they are functionally equivalent. In my tests, however, I
> > found that innerText seems not to be supported either. See the
> > following code, which is the same as the original, but with innerText
> > used as a replacement for textContent.
>
> Is it the case that setting el.innerText and el.textContent for a
> TextNode is the same as setting its value and setting the innerText
> and textContent for an element el is the same as
>
>   newText = String(newText);
>   while (el.firstChild) { el.removeChild(el.firstChild); }
>   if (newText !== '') {
>     el.appendChild(el.ownerDocument.createTextNode(newText));
>   }
>
> assuming ownerDocument and appendChild are reliable?
>
>
>
> > <div id="test">never set; neither innerText nor innerHTML worked</
> > div>
> > <script type="text/javascript">
> >    var testDiv = document.getElementById('test');
> >    testDiv.innerHTML = 'set using innerHTML, but innerText does not
> > work';
> >    testDiv.innerHTML += testDiv.innerText ? '' : '; in fact the
> > innerText property is unreadable';
> >    testDiv.innerText = 'setting innerText works' +
> > (testDiv.innerText ? '; so does reading innerText' : '');
> > </script>
>
> > On Jun 17, 7:00 pm, Mike Samuel <[email protected]> wrote:
> >> How does textContent compare to innerText?
>
> >> 2010/6/17 Michael Ficarra <[email protected]>:
>
> >> > Node.textContent, as defined by the W3C at
> >> >http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent
> >> > and by Mozilla athttps://developer.mozilla.org/En/DOM/Node.textContent
> >> > seems to be unsupported in cajoled code, even though it's commonly
> >> > used as a safe way to set (and retrieve) the textual content of a
> >> > node. A proof-of-concept follows.
>
> >> > <div id="test">never set; neither textContent nor innerHTML worked</
> >> > div>
> >> > <script type="text/javascript">
> >> >    var testDiv = document.getElementById('test');
> >> >    testDiv.innerHTML = 'set using innerHTML, but textContent does not
> >> > work';
> >> >    testDiv.innerHTML += testDiv.textContent ? '' : '; in fact the
> >> > textContent property is unreadable';
> >> >    testDiv.textContent = 'setting textContent works' +
> >> > (testDiv.textContent ? '; so does reading textContent' : '');
> >> > </script>

Reply via email to