Element in a td accessing parent <tr> element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    #1

    Element in a td accessing parent <tr> element

    I have an HTMLInputElemen t in a <td>. I want to access all the <td>'s
    in the same row as the input element.

    var tr=theInputElem ent.parentNode. parentNode;

    Why isn't the input element's parent's parent the <tr> node in
    Firefox? Internet Explorer seems to think so. How can I get the <tr>
    node (so I can iterate through its children) in Firefox?

    Why does this stuff have to be such a humongous god-forsaken
    nightmare?? (sorry, working-late-bitterness)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Matt Kruse

    #2
    Re: Element in a td accessing parent &lt;tr&gt; element

    Christopher Benson-Manica wrote:[color=blue]
    > var tr=theInputElem ent.parentNode. parentNode;[/color]

    You can always use a general-purpose function like this:

    function getParentByTagN ame(obj, tagName) {
    tagName = tagName.toLower Case();
    while (obj!= null && obj.tagName!=nu ll && obj.tagName.toL owerCase() !=
    tagName) {
    obj=obj.parentN ode;
    }
    return obj;
    }

    then do:

    var tr = getParentByTagN ame(theInputEle ment,"tr");

    --
    Matt Kruse



    Comment

    • Richard Cornford

      #3
      Re: Element in a td accessing parent &lt;tr&gt; element

      Christopher Benson-Manica wrote:[color=blue]
      > I have an HTMLInputElemen t in a <td>. I want to access
      > all the <td>'s in the same row as the input element.
      >
      > var tr=theInputElem ent.parentNode. parentNode;
      >
      > Why isn't the input element's parent's parent the <tr>
      > node in Firefox? Internet Explorer seems to think so.[/color]

      In the DOM generated from valid HTML in Mozilla/Firefox the parent of
      the parent of the child of a TD will be a TR. If that is not the case
      then my guess (and it will have to be a guess because you have not
      posted the mark-up with which the script is interacting) is that the
      FORM element has been placed in an invalid location in the TABBLE and
      you are experiencing differences in the DOM resulting from differences
      in HTML error correction strategies. As no standards could be applied to
      error-correction strategies, and the approach taken by any individual
      browser (at least the commercial ones) are not published so cannot be
      reproduced in other browsers, there is no reason to expect any two
      browsers to generate structurally similar DOMs from invalid HTML.
      [color=blue]
      > How can I get the <tr> node (so I can
      > iterate through its children) in Firefox?[/color]

      If the HTML is valid then Matt's approach will work, as should your
      original. But if the HTML is not valid then Mozilla/Firefox's DOM will
      be determined by the actual HTML used and no answer can be suggested
      without seeing the mark-up.
      [color=blue]
      > Why does this stuff have to be such a humongous god-forsaken
      > nightmare?? (sorry, working-late-bitterness)[/color]

      Problems in scripting that follow from invalid HTML are usually the
      result of assuming that because various browsers happily display the
      page in a more or less consistent way they will also produce
      structurally similar DOMs from that mark-up. If the result is a
      nightmare it is a nightmare of the authors own creation, and there is no
      shortage of advice to use valid HTML when attempting to make cross
      browser web pages (though it is often not made clear that it is in the
      scripting of those web pages that value of valid mark-up is most
      aparent).

      Richard.


      Comment

      • Christopher Benson-Manica

        #4
        Re: Element in a td accessing parent &lt;tr&gt; element

        Richard Cornford <Richard@litote s.demon.co.uk> spoke thus:
        [color=blue]
        > If the HTML is valid then Matt's approach will work, as should your
        > original. But if the HTML is not valid then Mozilla/Firefox's DOM will
        > be determined by the actual HTML used and no answer can be suggested
        > without seeing the mark-up.[/color]

        The actual markup was a little too complicated to post. In any case
        the markup passed the W3C validator, so I'm not sure what could have
        been wrong with it. I appreciate your judicious response to my
        frustration.

        --
        Christopher Benson-Manica | I *should* know what I'm talking about - if I
        ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        Working...