I have searched and found similar answers to this but not exactly and I can't figure out what I'm doing wrong... Thanks for any help you can provide!
<html><head>
<script>
function doTheInsert() {
var newRow=document.getElementById('myTable').insertRow();
newRow="<td>New row text</td><td>New row 2nd cell</td>";
}
</script></head>
<body>
<table id="myTable" border="1">
<tr>
<td>First row</td>
<td>First row 2nd cell</td>
</tr>
<tr>
<td>Second row</td>
<td>more stuff</td>
</tr>
</table>
<br>
<input type="button" onclick="doTheInsert()" value="Insert new row">
</body></html>
I suppose it would be more formally correct to use insertCell
for each added cell, but just dropping in the whole string will work if you set newRow's innerHTML:
function doTheInsert() {
var newRow=document.getElementById('myTable').insertRow();
// newRow = "<td>New row text</td><td>New row 2nd cell</td>"; <-- won't work
newRow.innerHTML = "<td>New row text</td><td>New row 2nd cell</td>";
}
function doTheInsert() {
var newRow=document.getElementById('myTable').insertRow();
newRow.innerHTML="<td>New row text</td><td>New row 2nd cell</td>";
}
<table id="myTable" border="1">
<tr>
<td>First row</td>
<td>First row 2nd cell</td>
</tr>
<tr>
<td>Second row</td>
<td>more stuff</td>
</tr>
</table>
<input type="button" onclick="doTheInsert()" value="Insert new row">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With