add row to table from child window

Discussion in 'Javascript' started by mike, Oct 6, 2005.

  1. mike

    mike Guest

    I am adding a row to a table from a child window that opened from the
    parent window.

    My js looks like:

    if ( self.opener.document.update )
    {
    var rows = self.opener.document.getElementById("A_Table");
    myTR = self.opener.document.createElement("TR");
    myTD=self.opener.document.createElement("TD");
    myTEXT =
    self.opener.document.createTextNode("test"); myTD.appendChild(myTEXT); myTR.appendChild(myTD); rows.appendChild(myTR);
    }

    This looks like it is executing, but i don't get anything showing up in
    my parent window in the table. The table exists with an id="A_Table".

    I'm concerned about the statement "rows.appendChild(myTR);"

    Is this correct?

    Mike
    mike, Oct 6, 2005
    #1
    1. Advertising

  2. mike

    RobG Guest

    mike wrote:
    > I am adding a row to a table from a child window that opened from the
    > parent window.
    >
    > My js looks like:
    >
    > if ( self.opener.document.update )
    > {
    > var rows = self.opener.document.getElementById("A_Table");


    From the ID, it seems 'rows' is a reference to a table. That is
    unfortunate since the table has a rows collection. I'd call that
    variable 'aTable' or similar, 'cos now your reference to the rows
    collection will be:

    var rowsCollection = rows.rows;

    > myTR = self.opener.document.createElement("TR");
    > myTD=self.opener.document.createElement("TD");
    > myTEXT =
    > self.opener.document.createTextNode("test"); myTD.appendChild(myTEXT); myTR.appendChild(myTD); rows.appendChild(myTR);
    > }


    Do myTR, myTD, myTEXT need to be global? Better to keep them local.

    >
    > This looks like it is executing, but i don't get anything showing up in
    > my parent window in the table. The table exists with an id="A_Table".


    I'll guess that you are using IE, 'cos it refuses to add rows to the
    table element, you have to add them to the tbody. A tbody is mandatory
    but you don't have to include the tags in the source HTML, your browser
    will insert it anyway.

    So either:

    - code a tbody element in your HTML and use that exactly as you are
    currently using the table

    - use insert row instead of appendChild

    - get the last row and add the new row as the next sibling, e.g.

    var lastRow = rows[rows.length-1];
    lastRow.parentNode.appendChild(myTR);


    Since the lastRow's parent will be the tbody, you don't need to worry
    about whether it has been coded in the HTML or change your use of the
    table ID.

    >
    > I'm concerned about the statement "rows.appendChild(myTR);"
    >
    > Is this correct?


    Yes, you are correct to be concerned about it. :)



    --
    Rob
    RobG, Oct 7, 2005
    #2
    1. Advertising

  3. mike

    RobG Guest

    RobG wrote:
    [...]
    > So either:
    >
    > - code a tbody element in your HTML and use that exactly as you are
    > currently using the table
    >
    > - use insert row instead of appendChild
    >
    > - get the last row and add the new row as the next sibling, e.g.
    >
    > var lastRow = rows[rows.length-1];
    > lastRow.parentNode.appendChild(myTR);


    Aggh, fell into my own trap!!

    var lastRow = rows.rows[rows.rows.length-1];
    lastRow.parentNode.appendChild(myTR);

    See, using aTable (or I guess myTable) instead of 'rows' would be clearer:

    var lastRow = myTable.rows[myTable.rows.length-1];
    lastRow.parentNode.appendChild(myTR);


    [...]


    --
    Rob
    RobG, Oct 7, 2005
    #3
  4. mike wrote:
    > I am adding a row to a table from a child window that
    > opened from the parent window.
    >
    > My js looks like:
    >
    > if ( self.opener.document.update )
    > {
    > var rows = self.opener.document.getElementById("A_Table");


    Is this "A_Table" a TABLE or a TBODY, THEAD or TFOOT element?

    > myTR = self.opener.document.createElement("TR");

    <snip>
    > rows.appendChild(myTR); }

    <snip>

    A TR element is not allowed as a child of a TABLE element in HTML (see
    the HTML DTDs for details).

    Richard.
    Richard Cornford, Oct 7, 2005
    #4
  5. mike

    mike Guest

    That is absolutly correct. I don't have a tbody element and I am using
    IE. Let me make a change and see if that helps.
    mike, Oct 7, 2005
    #5
  6. mike

    mike Guest

    Well, That worked. I appreciate it.

    It was confusing, because I have some other code like this where it is
    in the same document and rows.appendChild(myTR); worked albeit with a
    tbody defined in the document.

    Thanks, Mike
    mike, Oct 7, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bhchng
    Replies:
    0
    Views:
    641
    bhchng
    Jan 13, 2004
  2. phl
    Replies:
    1
    Views:
    4,323
    Martin Jay
    Jun 8, 2006
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,079
    David Schwartz
    Dec 9, 2003
  4. Spammay Blockay
    Replies:
    1
    Views:
    401
    Dietmar Meier
    Feb 4, 2005
  5. Replies:
    4
    Views:
    145
Loading...

Share This Page