Cloning rows works -- how to alter cells?

R

Rich_C

I'm able to clone a table row using this:

function insert(btn) {var cell, newRow, row, sect;
if((cell = btn.parentNode) && (row = cell.parentNode) &&
row.cloneNode && (sect = row.parentNode) && sect.insertBefore)
{
newRow = row.cloneNode(true);
/* If you need to alter the new row
* or its contents, do it here.
*/

sect.insertBefore(newRow, row.nextSibling);
}
}

the problem is that I need to change contents of 2 cells. One cell
contains an input box that should be blank in the new row, one cell
contains an input box that shoule be reset to 1 in the new row. The
script indicates that I can 'alter the new row', but how do I access
the cell contents to make changes?

Also, the button to add rows is in the last column of the row. Is
there a way to remove (or hide) it in all but the last row?

Thanks,
Rich
 
M

Martin Honnen

Rich_C wrote:

newRow = row.cloneNode(true);
the problem is that I need to change contents of 2 cells. One cell
contains an input box that should be blank in the new row, one cell
contains an input box that shoule be reset to 1 in the new row. The
script indicates that I can 'alter the new row', but how do I access
the cell contents to make changes?

The row element object has a cells collection so you can access e.g.
newRow.cells
newRow.cells.length
newRow.cells[0] // first cell
and then if you access a cell you have the DOM with childNodes, with
getElementsByTagName to access stuff in the cell.
 
R

Rich_C

Thanks Martin --
I'm getting 'cells.4' is null or not an object in IE when I try

newRow.cells[4].getElementsByTagName('input')[0].value =1;

any idea what's not right?




Martin said:
Rich_C wrote:

newRow = row.cloneNode(true);
the problem is that I need to change contents of 2 cells. One cell
contains an input box that should be blank in the new row, one cell
contains an input box that shoule be reset to 1 in the new row. The
script indicates that I can 'alter the new row', but how do I access
the cell contents to make changes?

The row element object has a cells collection so you can access e.g.
newRow.cells
newRow.cells.length
newRow.cells[0] // first cell
and then if you access a cell you have the DOM with childNodes, with
getElementsByTagName to access stuff in the cell.
 
M

Martin Honnen

Rich_C wrote:

I'm getting 'cells.4' is null or not an object in IE when I try

newRow.cells[4].getElementsByTagName('input')[0].value =1;

Well what is newRow.cells.length? Are there at least 5 cells? The index
starts with 0 so the first cell is newRow.cells[0].
 
R

Rich_C

there are 16 cells

Martin said:
Rich_C wrote:

I'm getting 'cells.4' is null or not an object in IE when I try

newRow.cells[4].getElementsByTagName('input')[0].value =1;

Well what is newRow.cells.length? Are there at least 5 cells? The index
starts with 0 so the first cell is newRow.cells[0].
 
M

Martin Honnen

Rich_C said:
there are 16 cells

Can you post a URL where the error occurs?
What happens if you try the script with other browsers, does that work
or do you get an error too?
 
R

Rich_C

It's an intranet site, so no public access.

it works in Netscape, but causes other things to break on this page and
throughout the site. We wrote for IE only since it's written for
internal users and we can specifiy the browser.
 
R

Rich_C

Martin,

Finally got this to work.. thanks for all your help!

apparently IE won't allow a change until after it's been written. I
switched these two lines and it works perfectly.

sect.insertBefore(newRow, row.nextSibling);

newRow.cells[4].getElementsByTagName("input")[0].value = 1;
 

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
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top