getElementsByTagName 'n such

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

    getElementsByTagName 'n such

    hi folks,

    OK, so let's say, for example, I have a bit of HTML that looks like
    this:

    <td class="regular1 b" valign="top">
    <a href="notfound. html"><span class="list5">< b>Lecture
    V</b></span></a>
    </td>

    And I want to save all the text ("all" meaning the tags and
    everything) between the <td> and </td>. Using JavaScript, I was able
    to isolate the <td></td> by doing:

    var w = myTable.getElem entsByTagName(" TD");

    So then I have an IF statement within a FOR loop that looks like:

    if (w.item(i).clas sName == "regular1b" )
    alert(w[i].childNodes[0].nodeValue);

    The ALERT() is just a place holder to make sure things are working.
    The thing is, nodeValue returns NULL because there's no actual text
    within the <td></td> tags; the only thing there is more HTML code, and
    the text between the <span></span> apparently isn't considered part of
    the <td></td> tags.

    I guess I'm wondering if there's another way to go about getting the
    text from in between the <td></td> tags short of just doing a
    brute-force text search on the whole darn page. Any help would be
    much appreciated...

    Fleet
  • Ron

    #2
    Re: getElementsByTa gName 'n such

    norfleet wrote:
    [color=blue]
    >hi folks,
    >
    >OK, so let's say, for example, I have a bit of HTML that looks like
    >this:
    >
    ><td class="regular1 b" valign="top">
    > <a href="notfound. html"><span class="list5">< b>Lecture
    >V</b></span></a>
    ></td>
    >
    >And I want to save all the text ("all" meaning the tags and
    >everything) between the <td> and </td>. Using JavaScript, I was able
    >to isolate the <td></td> by doing:
    >
    >var w = myTable.getElem entsByTagName(" TD");
    >
    >So then I have an IF statement within a FOR loop that looks like:
    >
    >if (w.item(i).clas sName == "regular1b" )
    > alert(w[i].childNodes[0].nodeValue);
    >
    >The ALERT() is just a place holder to make sure things are working.
    >The thing is, nodeValue returns NULL because there's no actual text
    >within the <td></td> tags; the only thing there is more HTML code, and
    >the text between the <span></span> apparently isn't considered part of
    >the <td></td> tags.
    >
    >I guess I'm wondering if there's another way to go about getting the
    >text from in between the <td></td> tags short of just doing a
    >brute-force text search on the whole darn page. Any help would be
    >much appreciated...
    >
    >Fleet
    >
    >[/color]
    Heya Fleet,
    Unless the document is normalized, childNodes[0] may be a whitespace
    text node. You might want to normalize your TD before reading from it.
    In addition, nodeValue is supposed to return null for any element node
    ->

    .. Unfortunately, the best (possibly only) current way to get what you
    want is to use the non-standard innerHTML property of your TD object. It
    is implemented in the latest versions of IE and Gecko-based browsers.

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: getElementsByTa gName 'n such

      norfleet wrote:
      [color=blue]
      > OK, so let's say, for example, I have a bit of HTML that looks like
      > this:
      >
      > <td class="regular1 b" valign="top">
      > <a href="notfound. html"><span class="list5">< b>Lecture
      > V</b></span></a>
      > </td>
      >
      > And I want to save all the text ("all" meaning the tags and
      > everything) between the <td> and </td>. Using JavaScript, I was able
      > to isolate the <td></td> by doing:
      >
      > var w = myTable.getElem entsByTagName(" TD");
      >
      > So then I have an IF statement within a FOR loop that looks like:
      >
      > if (w.item(i).clas sName == "regular1b" )[/color]

      As you have seen, there is no need to call the item() method explicitely
      when accessing the DOM with an ECMAScript implementation. Using the square
      bracket property accessor syntax, that method or the namedItem() method
      is called implicitely, depending on the type of the operand.

      <http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>
      [color=blue]
      > alert(w[i].childNodes[0].nodeValue);
      >
      > The ALERT() is just a place holder to make sure things are working.
      > The thing is, nodeValue returns NULL because there's no actual text
      > within the <td></td> tags;[/color]

      It returns `null' (ECMAScript is case-sensitive) because the first child
      node is an element node. This is documented and standards compliant
      behavior. Think of the contents of the "td" element as a subtree where
      nested content is a child node. Provided that the whitespace after the
      start tag of the "td" element and before the start tag of the "a" element
      is not considered a text node (proprietary behavior!), this subtree looks like

      ..
      ..
      ..
      '- TD class="regular1 b" valign="top"
      | |
      | '- A href="notfound. html"
      | |
      | '- SPAN class="list5"
      | |
      | '- B
      | |
      | '- TEXT "Lecture V"
      |
      |- ...
      ..
      ..
      ..

      (The "Show parse tree" feature of the W3C Validator
      <http://validator.w3.or g/> provides a similar presentation.)

      You see that childNodes[0] or firstChild refers to an element node.

      Standard compliant parsing would result in

      ..
      ..
      ..
      '- TD class="regular1 b" valign="top"
      | |
      | |- TEXT "\n\t"
      | |
      | '- A HREF="notfound. html"
      | |
      | '- SPAN class="list5"
      | |
      | '- B
      | |
      | '- TEXT "Lecture V"
      |
      |- ...
      ..
      ..
      ..

      so in Mozilla/5.0 (Mozilla, Netscape 6+, Firefox, Camino,
      ....) you get "\n\t" for childNodes[0].nodeValue.

      That is why it was suggested to normalize the document, such as

      <td class="regular1 b" valign="top"><a
      href="notfound. html"[color=blue]
      ><span class="list5"
      ><b>Lecture V</b></span></a></td>[/color]
      [color=blue]
      > the only thing there is more HTML code, and the text between the
      > <span></span> apparently isn't considered part of the <td></td> tags.[/color]

      That misconception is the main cause for your problem.
      [color=blue]
      > I guess I'm wondering if there's another way to go about getting the
      > text from in between the <td></td> tags short of just doing a
      > brute-force text search on the whole darn page. Any help would be
      > much appreciated...[/color]

      There is. The "innerHTML" property has been suggested. But since it is
      proprietary, and you are using the standards compliant DOM, you should
      rather serialize the subtree, traversing it. Depending on the UA's DOM,
      there are predefined serializer objects, such as XMLSerializer in the
      Gecko DOM. But you can code your own serializer as well.


      PointedEars

      Comment

      • DU

        #4
        Re: getElementsByTa gName 'n such

        norfleet wrote:
        [color=blue]
        > hi folks,
        >
        > OK, so let's say, for example, I have a bit of HTML that looks like
        > this:
        >
        > <td class="regular1 b" valign="top">
        > <a href="notfound. html"><span class="list5">< b>Lecture
        > V</b></span></a>
        > </td>
        >
        > And I want to save all the text ("all" meaning the tags and
        > everything) between the <td> and </td>. Using JavaScript, I was able
        > to isolate the <td></td> by doing:
        >
        > var w = myTable.getElem entsByTagName(" TD");
        >
        > So then I have an IF statement within a FOR loop that looks like:
        >
        > if (w.item(i).clas sName == "regular1b" )
        > alert(w[i].childNodes[0].nodeValue);
        >
        > The ALERT() is just a place holder to make sure things are working.
        > The thing is, nodeValue returns NULL because there's no actual text
        > within the <td></td> tags; the only thing there is more HTML code, and
        > the text between the <span></span> apparently isn't considered part of
        > the <td></td> tags.
        >
        > I guess I'm wondering if there's another way to go about getting the
        > text from in between the <td></td> tags[/color]




        There is. The textContent attribute in the Node interface (DOM 3 Core)
        is supported by Mozil1a 1.5+. I tried it with your specific markup code
        (with all the white-space, line feed, etc) and it worked without a
        problem. I tried it with more complex subtree and it worked as expected.

        Bug 210451: Implement Node.textConten t
        RESOLVED (caillon) in Core - DOM: Core & HTML. Last updated 2008-07-31.




        For other browsers not supporting DOM 3 Node Interface, you can create a
        traversal subtree function and get/fetch the text or use the
        non-standard innerHTML attribute.

        DU



        short of just doing a[color=blue]
        > brute-force text search on the whole darn page. Any help would be
        > much appreciated...
        >
        > Fleet[/color]

        Comment

        • DU

          #5
          Re: getElementsByTa gName 'n such

          norfleet wrote:
          [color=blue]
          > hi folks,
          >
          > OK, so let's say, for example, I have a bit of HTML that looks like
          > this:
          >
          > <td class="regular1 b" valign="top">
          > <a href="notfound. html"><span class="list5">< b>Lecture
          > V</b></span></a>
          > </td>
          >
          > And I want to save all the text ("all" meaning the tags and
          > everything) between the <td> and </td>. Using JavaScript, I was able
          > to isolate the <td></td> by doing:
          >
          > var w = myTable.getElem entsByTagName(" TD");
          >
          > So then I have an IF statement within a FOR loop that looks like:
          >
          > if (w.item(i).clas sName == "regular1b" )
          > alert(w[i].childNodes[0].nodeValue);
          >
          > The ALERT() is just a place holder to make sure things are working.
          > The thing is, nodeValue returns NULL because there's no actual text
          > within the <td></td> tags; the only thing there is more HTML code, and
          > the text between the <span></span> apparently isn't considered part of
          > the <td></td> tags.
          >[/color]

          I suggest you play around, get to know, get accustomed to using
          Mozilla's DOM inspector. You can install it on Netscape 7.1 and Firefox
          0.8 as well. This is how I personally noticed that white-space between
          nodes are treated as anonymous text nodes. What you say above is not
          true (your misconception is widely common) and was explained in

          Whitespace in the DOM
          The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps.


          DU
          [color=blue]
          > I guess I'm wondering if there's another way to go about getting the
          > text from in between the <td></td> tags short of just doing a
          > brute-force text search on the whole darn page. Any help would be
          > much appreciated...
          >
          > Fleet[/color]

          Comment

          Working...