I've been scratching my head about why my code is acting the way it is.
My question is that why when I add a table row using my function addRow, it resets all of the input values of previous rows?
Below is a code snippet displaying my issues..
function addRow() {
//the html for adding a row (contains the row tag and inputs)
htmlString = '<tr><td><input type="text"></input></td></tr>';
//add the html string to the tbody of the tableTestSamples
document.getElementById("testTable").getElementsByTagName("tbody")[0].innerHTML += htmlString;
}
<table id="testTable">
<tbody>
<tr>
<td>
<input type="text"></input>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>
<input type="button" onclick="addRow()" value="Add Row"></input>
</td>
</tr>
</tfoot>
</table>
It adds a row.. except its resetting any previously entered values. Why is that?
Thanks!
innerHTMLof an element causes it to replace all existing children. Try to useappendChild()instead.