Search code examples
javascriptfirefoxclonenode

Javascript cloneNode issue in FireFox


The JavaScript below works great in Internet Explorer, but doesnt work in FireFox.

It gets hung up at "NewField is undefined" which would be the line:

for (var i=0;i<NewField.length;i++)

That is the loop that will rename the form fields on that table row.

Here is where you can see the entire page http://www.sorenwinslow.com/CloneRowTest.asp

function CloneRows(TableRowId)
{
var NumRows = document.forms["TestForm"].NumRows.value;
NumRows++;
document.forms["TestForm"].NumRows.value = NumRows;
var RowToClone = document.getElementById(TableRowId);
var NewTableRow = RowToClone.cloneNode(true);
NewTableRow.id = TableRowId + NumRows ;
NewTableRow.style.display = "table-row";
var NewField = NewTableRow.all;
for (var i=0;i<NewField.length;i++)
{
    var theName = NewField[i].name;
    if (theName)
    {
        NewField[i].name = theName + NumRows;
    }
}
var insertHere = document.getElementById(TableRowId);
insertHere.parentNode.insertBefore(NewTableRow,insertHere);
}

Solution

  • .all does generally not exist, only document.all, but it isn't even standard. Internet Explorer has some custom properties but you should not rely on them. You probably want .children:

    var NewField = NewTableRow.children;
    

    or .cells:

    var NewField = NewTableRow.cells;