How do I get an element's the text node

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

    How do I get an element's the text node

    I want if possible to use something like:

    element.getElem entsByTagName(' text')

    but that doesn't work. Is there another value for the parameter, or is it
    not possible with getElementsByTa gName?

    Thanks,
    Ron



  • Mick White

    #2
    Re: How do I get an element's the text node

    Ron Brennan wrote:
    [color=blue]
    > I want if possible to use something like:
    >
    > element.getElem entsByTagName(' text')
    >
    > but that doesn't work. Is there another value for the parameter, or is it
    > not possible with getElementsByTa gName?
    >[/color]

    As far as I know, no, unless you have a tag named <text>, in which case
    "element.getEle mentsByTagName( 'text')" returns a collection of <text>
    elements contained in "element"

    Mick

    Comment

    • Yann-Erwan Perio

      #3
      Re: How do I get an element's the text node

      Ron Brennan wrote:
      [color=blue]
      > I want if possible to use something like:
      >
      > element.getElem entsByTagName(' text')
      >
      > but that doesn't work. Is there another value for the parameter, or is it
      > not possible with getElementsByTa gName?[/color]

      As Mick said you cannot do it with getElementsByTa gName, this method
      returns *elements*, i.e. nodes of type "element", and not other types of
      nodes (at least it shouldn't).

      If you want to retrieve only text nodes, then you'll have to build
      custom node iterators; depending on your requirements this may not be
      too difficult.

      FYI the W3C has defined traversal objects, namely Treewalkers or
      NodeIterators, which permit to traverse the tree in a certain order,
      with node filters, and much more. Unfortunately these are not supported
      in IE (and probably lots of other browsers), which makes the objects
      unusable for the web.

      <URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/traversal.html>

      Try the following in Opera 8 or Mozilla 1.7+ to get an illustration.

      ---
      <div id="foo">Hello, <span>World</span>!</div>

      <script type="text/javascript">
      if(
      document.getEle mentById &&
      document.create TreeWalker &&
      typeof NodeFilter!="un defined"
      ){

      var tw=document.cre ateTreeWalker(
      document.getEle mentById("foo") ,
      NodeFilter.SHOW _TEXT,
      null,
      false
      );
      var node;
      var buf=[];

      while(node=tw.n extNode())
      buf.push(node.n odeValue);

      alert(buf.lengt h ? buf.join("\n") : "No text node iterated");
      }
      </script>
      ---


      HTH,
      Yep.

      Comment

      • Ron Brennan

        #4
        Re: How do I get an element's the text node

        Yup, it appears impossible that way. The following is an acceptable
        solution:

        var text = getElementById( "tdElement").fi rstChild.data;



        Comment

        • VK

          #5
          Re: How do I get an element's the text node

          > The following is an acceptable solution:[color=blue]
          > var text = getElementById( "tdElement").fi ­rstChild.data;[/color]

          If you're working with your own pages where you're the king.

          Universally *in HTML* there is no guarantee that the firstChild will
          always point to the text node (even if you see nothing but text in the
          element of question). It may return an empty text node created say on
          place of the page break.

          Comment

          • Danny

            #6
            Re: How do I get an element's the text node


            If you're looking for the text of the node with no html and all
            concatenated in one, so called NORMALIZED()'ed node, for mozilla
            obj.textContent gives you all that, stripped of all html and any null
            chars like tabs and CR's, for IE/opera you can use obj.innerText, is
            exactly the same object. Now, if you want to do it per node over the
            childNodes collection, then you can iterate of the object you want to
            check, check if its obj.childNodes. length is greater than 0, or has
            children, then check each of its children for obj.childNodes. length as
            well, for any children in them. Anyhow, in the loop, check each
            obj.nodeType, #text nodes have a DOM nodeType of 3, elements are 1,
            attributes are 2 and so on, check your DOM glossary if you wish.


            Danny

            On Sat, 02 Jul 2005 10:55:08 -0700, Ron Brennan <rbrennan@magma .ca> wrote:


            --
            Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: How do I get an element's the text node

              VK wrote:
              [color=blue][color=green]
              >> The following is an acceptable solution:
              >> var text = getElementById( "tdElement").fi ­rstChild.data;[/color]
              >
              > If you're working with your own pages where you're the king.[/color]

              No, because it has to be `document.getEl ementById'.
              [color=blue]
              > Universally *in HTML* there is no guarantee that the firstChild will
              > always point to the text node (even if you see nothing but text in the
              > element of question).[/color]

              Correct, the text may be marked up by another element.
              [color=blue]
              > It may return an empty text node created[/color]

              There is no such thing as an empty text node in an unchanged DOM tree;
              either the text node contains whitespace or the (first child node) node
              is an element node.
              [color=blue]
              > say on place of the page break.[/color]

              What is a page break in HTML?


              PointedEars

              Comment

              Working...