By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

add row to table from child window

P: n/a
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

Oct 6 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Oct 6 '05 #2

P: n/a
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
Oct 6 '05 #3

P: n/a
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.
Oct 6 '05 #4

P: n/a
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.

Oct 7 '05 #5

P: n/a
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

Oct 7 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.