add row to table from child window

M

mike

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
 
R

RobG

mike said:
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. :)
 
R

RobG

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);


[...]
 
R

Richard Cornford

mike said:
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");
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.
 
M

mike

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.
 
M

mike

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top