innerText vs textContent

243 views
Skip to first unread message

Ngan Pham

unread,
Oct 23, 2009, 5:15:58 PM10/23/09
to Prototype: Core
Hey guys,

Unless I'm mistaken, there seems to be cross browser incompatibility issue with innerText and textContent.  innerText is IE (and everyone else I believe), and textContent is Firefox (only, i believe).  In anycase, wouldn't it make sense for Prototype to smooth this out?

Element.getText() perhaps? 

kangax

unread,
Oct 23, 2009, 7:14:14 PM10/23/09
to Prototype: Core
That's actually one big can of worms. Here's a full explanation, if
you're interested — http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox/1359822#1359822

Most robust solution is actually not `textContent` and not
`innerText`, but recursive iteration of children (text) nodes. However
that could be "slow", depending on a context where it is to be used.
It is, of course, possible to feature test all known discrepancies and
craft fast, yet compliant implementation, but the size of such
solution would be rather humongous (see, for example, one of my
(incomplete) attempts — http://groups.google.com/group/comp.lang.javascript/msg/d1574256dea74077).

As always, it's all about context. If you care about consistent
results, go for recursive iteration or innerHTML + regex tag stripping
(remember that regex would still be fragile). If, on the other hand,
it's all about speed — use textContent/innerText and take care of
discrepancies as needed.

--
kangax
Reply all
Reply to author
Forward
0 new messages