On Aug 25, 9:31 pm, Daniel Rubin <[email protected]> wrote:
> david wrote:
> > Hi molo,
>
> > I think the innerHTML is the simpliest way to grab text inside a node.
No, it's not. If you want just the text, use textContent or innerText
as appropriate depending on which property the host supports (some
support both).
[...]
> > On the inconstency of innerHTML method across browser, I think there
> > is only on the way browser treat "return" inside an HTML file as or
> > not as a text node, but if any other inconsistency exist, ligth my
> > mind :)
There are many other inconsistencies, mostly related to attributes as
far as I remember, so likely not important in this case but still a
good reason to avoid innerHTML for reading from the DOM in anything
other than trivial cases.
> There's the textContent Property, too, which *is* inconsistent,
> especially regarding that it is called innerText in IE. I don't think
> it's part of any standard at all.
They are completely different things that do much the same job.
innerText is an IE proprietary property that has been copied by some
other browsers, textContent is a W3C DOM 3 property:
innerText property:
<URL: http://msdn.microsoft.com/en-us/library/ms533899(VS.85).aspx >
textContent property:
<URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent
>
> I have sometimes used it like this:
> var text = elem.textContent || elem.innerText;
Consider instead:
var text;
if (typeof elem.textContent == 'string') {
text = elem.textContent;
} else if (typeof elem.innerText == 'string') {
text = elem.innerText;
}
But that will fail in certain cases in some browsers. I think the only
really reliable method is to recurse over the descendant elements and
extract the text (which in the case of a node with only one or two
text nodes as children is pretty fast).
> It has the advantage of filtering out all non-text stuff. But that
> doesn't seem to be needed in this special case, so it's probably
> worthless for you.
It depends on how robust the OP wants the function to be.
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---