f



How do I get the value of a text node?

Hi,

If I have a TD, whose id = "myTd," that only contains text within
it,how do extract that text?

Thanks, - Dave
0
laredotornado
7/21/2008 7:51:20 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

4 Replies
469 Views

Similar Articles

[PageSpeed] 10

laredotornado <laredotornado@zipmail.com> writes:

> If I have a TD, whose id = "myTd," that only contains text within
> it,how do extract that text?

Find all child nodes that are text nodes, and extract their content.
Even if the td only contains text, that text might be split over
more than one text node (worst case).

var td = document.getElementById("myTd");
var textNodeContents = [];
for(var chld = td.firstChild; chld; chld = chld.nextSibling) {
  if (chld.nodeType == 3) { // text node
    textNodeContents.push(chld.nodeValue);
  }
}
var text = textNodeContents.join("");


/L
-- 
Lasse Reichstein Nielsen
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'
0
Lasse
7/21/2008 8:19:06 PM
On Jul 21, 2:19=A0pm, Lasse Reichstein Nielsen <l...@hotpop.com> wrote:
> laredotornado<laredotorn...@zipmail.com> writes:
> > If I have a TD, whose id =3D "myTd," that only contains text within
> > it,how do extract that text?
>
> Find all child nodes that are text nodes, and extract their content.
> Even if the td only contains text, that text might be split over
> more than one text node (worst case).
>
> var td =3D document.getElementById("myTd");
> var textNodeContents =3D [];
> for(var chld =3D td.firstChild; chld; chld =3D chld.nextSibling) {
> =A0 if (chld.nodeType =3D=3D 3) { // text node
> =A0 =A0 textNodeContents.push(chld.nodeValue);
> =A0 }}
>
> var text =3D textNodeContents.join("");
>
> /L
> --
> Lasse Reichstein Nielsen
> =A0DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.=
html>
> =A0 'Faith without judgement merely degrades the spirit divine.'

I'm noticing some odd behavior.  When I call

var ele =3D document.getElementById(id);
var innerHtml =3D ele.innerHTML;
var text =3D ele.nodeValue;

The value of "innerHtml" yields a value whereas the value of "text"
comes up empty.  The object type of ele is [object
HTMLTableCellElement].

Any other insights are greatly appreciatd, - Dave
0
laredotornado
7/21/2008 10:11:40 PM
laredotornado <laredotornado@zipmail.com> writes:

> I'm noticing some odd behavior.  When I call
>
> var ele = document.getElementById(id);
> var innerHtml = ele.innerHTML;
> var text = ele.nodeValue;
>
> The value of "innerHtml" yields a value whereas the value of "text"
> comes up empty.  The object type of ele is [object
> HTMLTableCellElement].

That's because table cells, and in fact most HTML node types, do not
have a useful nodeValue property. HTML text is part of the value of
Text nodes, which are children of the cell (if there is any text).

IOW, any characters "in between" tags and some other special
constructs results in one or more Text nodes containing the character
data.

> Any other insights are greatly appreciatd, - Dave

See http://developer.mozilla.org/en/docs/DOM:element.nodeValue

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
0
Joost
7/21/2008 10:34:34 PM
Joost Diepenmaat <joost@zeekat.nl> writes:

> IOW, any characters "in between" tags and some other special
                                           ^
                                       not part of

> constructs results in one or more Text nodes containing the character
> data.

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
0
Joost
7/21/2008 10:35:33 PM
Reply: