Closed Bug 339766 Opened 18 years ago Closed 18 years ago

DOM: firstChild doesn't work every time

Categories

(Firefox :: General, defect)

defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 26179

People

(Reporter: dkrleza, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3

I was trying to create javascript function for copying table row with all contents and appending new row at the end of the table. It happends that is some cases (depends on HTML) firstChild doesn't return proper object inside table cell. Instead od object inside table cell, as first child is returned #text. Small changes in HTML code and everything works properly.

Reproducible: Always

Steps to Reproduce:
1. Make HTML code which contains
<table id="my_table">
<tr>
  <td>
    <input name="a1" value="Test1">
  </td>
</tr>
</table>

2. Make javascript function for copying table row
function copy_row() {
  var t=document.getElementById("my_table");
  var old_row=t.rows[0];
  var new_row=t.insertRow(1);
  var old_cell_contents=old_row.cells[0].firstChild;
  var new_cell_contents=old_cell_contents.cloneNode(true);
  new_cell_contents.value="Test2";
  new_cell_contents.name="a2";
  var new_cell=new_row.insertCell(0);
  new_cell.appendChild(new_cell_contents);
}

3. Make a button which will call javascript function
<button type="button" onclick="copy_row();">Click me</button>

4. Load this in your browser and click on button
Actual Results:  
Function copy_row is terminated on setting new text input object's value. cloneNode didn't clone old text input object because firstChild didn't return old text input object. Same happends when using childNodes[0].

Expected Results:  
After click on button and calling copy_row function, second row in table should appear with new text input object. New text input object with name a2 and value Test2.

If you change HTML code like:
<table id="my_table">
<tr>
  <td><input name="a1" value="Test1"></td>
</tr>
</table>

everthing works properly!

*** This bug has been marked as a duplicate of 26179 ***
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.