Referencing enclosing table

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

    Referencing enclosing table

    Hi:

    Is it possible to reference a table which contains a select list that
    has an event handler that modifies the table, without using the table
    ID?

    Example: <table id="SearchTable ">
    event handler in select list is
    onChange="addTa bleRow('SearchT able')"
    And in addTableRow(), I have the following code:
    var tbl = document.getEle mentById(which) ;
    // etc, etc, works fine BUT, is
    // there a component of the DOM that allows me to reference
    // the table as a parent?

    If so, pointers and URLs to appropriate documentation would
    suffice.

    Thanks
    tim
  • Jc

    #2
    Re: Referencing enclosing table

    Tim Johnson wrote:[color=blue]
    > // etc, etc, works fine BUT, is
    > // there a component of the DOM that allows me to reference
    > // the table as a parent?
    >
    > If so, pointers and URLs to appropriate documentation would
    > suffice.[/color]

    The parentNode property should suffice. See the FAQ for links to
    applicable documention.



    Comment

    • RobG

      #3
      Re: Referencing enclosing table

      Tim Johnson wrote:[color=blue]
      > Hi:
      >
      > Is it possible to reference a table which contains a select list that
      > has an event handler that modifies the table, without using the table
      > ID?
      >
      > Example: <table id="SearchTable ">
      > event handler in select list is
      > onChange="addTa bleRow('SearchT able')"
      > And in addTableRow(), I have the following code:
      > var tbl = document.getEle mentById(which) ;
      > // etc, etc, works fine BUT, is
      > // there a component of the DOM that allows me to reference
      > // the table as a parent?
      >
      > If so, pointers and URLs to appropriate documentation would
      > suffice.
      >
      > Thanks
      > tim[/color]


      <table ...>
      ...
      < ... onclick="getTab leParent(this)" ...>
      ...
      </table>


      <script type="text/javascript">
      function getTableParent( x){
      while ( "table" != x.nodeName.toLo werCase() && x.parentNode ) {
      x = x.parentNode
      }
      return ( "TABLE" == x.nodeName )? x : null;
      }
      </script>

      However, if you want to add rows, you must to add them to the tbody
      element, not the table element. Even if you don't put a tbody
      element in the HTML source, the browser will insert one at an
      appropriate place. IE will not add a row to a table element, you
      must add them to the tbody.

      Other browsers will cope with script adding rows to the body (they
      work out the tbody was intended), but not IE. If there are multiple
      tbody elements, clearly you want to target the right one.

      So the function should probably be:

      function getTbodyParent( x){
      while ( "tbody" != x.nodeName.toLo werCase() && x.parentNode ) {
      x = x.parentNode
      }
      return ( "tbody" == x.nodeName.toLo werCase() )? x : null;
      }

      And whatever function calls getTbodyParent should ensure that the
      return is not null.


      --
      Rob

      Comment

      • Mick White

        #4
        Re: Referencing enclosing table

        Tim Johnson wrote:[color=blue]
        > Hi:
        >
        > Is it possible to reference a table which contains a select list that
        > has an event handler that modifies the table, without using the table
        > ID?
        >
        > Example: <table id="SearchTable ">
        > event handler in select list is
        > onChange="addTa bleRow('SearchT able')"
        > And in addTableRow(), I have the following code:
        > var tbl = document.getEle mentById(which) ;
        > // etc, etc, works fine BUT, is
        > // there a component of the DOM that allows me to reference
        > // the table as a parent?
        >[/color]

        Climb the node tree until the nodeName == "TABLE"
        var t;
        while(t.parentN ode.tagName.toU pperCase()!="TA BLE"){
        t=t.parentNode;
        }

        Mick

        Comment

        • Mick White

          #5
          Re: Referencing enclosing table

          RobG wrote:
          [color=blue]
          > Tim Johnson wrote:[/color]
          [color=blue][color=green]
          >>
          >> Is it possible to reference a table which contains a select list that
          >> has an event handler that modifies the table, without using the table
          >> ID?[/color][/color]
          </table>
          [...][color=blue]
          >
          >
          > <script type="text/javascript">
          > function getTableParent( x){
          > while ( "table" != x.nodeName.toLo werCase() && x.parentNode ) {
          > x = x.parentNode
          > }
          > return ( "TABLE" == x.nodeName )? x : null;
          > }
          > </script>
          >[/color]
          [...]
          A much better explanation, Rob.
          Mick

          Comment

          • Tim Johnson

            #6
            Re: Referencing enclosing table

            Tim Johnson wrote:
            <...>[color=blue]
            > If so, pointers and URLs to appropriate documentation would
            > suffice.
            >[/color]
            Thanks to everyone for all the good input. I can mine the
            heck out of this stuff!
            -cheers-
            tim

            Comment

            Working...