How do I get the value of a text node?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado

    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
  • Lasse Reichstein Nielsen

    #2
    Re: How do I get the value of a text node?

    laredotornado <laredotornado@ zipmail.comwrit es:
    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.getEle mentById("myTd" );
    var textNodeContent s = [];
    for(var chld = td.firstChild; chld; chld = chld.nextSiblin g) {
    if (chld.nodeType == 3) { // text node
    textNodeContent s.push(chld.nod eValue);
    }
    }
    var text = textNodeContent s.join("");


    /L
    --
    Lasse Reichstein Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • laredotornado

      #3
      Re: How do I get the value of a text node?

      On Jul 21, 2:19 pm, Lasse Reichstein Nielsen <l...@hotpop.co mwrote:
      laredotornado<l aredotorn...@zi pmail.comwrites :
      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.getEle mentById("myTd" );
      var textNodeContent s = [];
      for(var chld = td.firstChild; chld; chld = chld.nextSiblin g) {
        if (chld.nodeType == 3) { // text node
          textNodeContent s.push(chld.nod eValue);
        }}
      >
      var text = textNodeContent s.join("");
      >
      /L
      --
      Lasse Reichstein Nielsen
       DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'
      I'm noticing some odd behavior. When I call

      var ele = document.getEle mentById(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
      HTMLTableCellEl ement].

      Any other insights are greatly appreciatd, - Dave

      Comment

      • Joost Diepenmaat

        #4
        Re: How do I get the value of a text node?

        laredotornado <laredotornado@ zipmail.comwrit es:
        I'm noticing some odd behavior. When I call
        >
        var ele = document.getEle mentById(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
        HTMLTableCellEl ement].
        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...ment.nodeValue

        --
        Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

        Comment

        • Joost Diepenmaat

          #5
          Re: How do I get the value of a text node?

          Joost Diepenmaat <joost@zeekat.n lwrites:
          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/

          Comment

          Working...