Victor Kimura
asked on
appendChild(clone) with new id at end of <tr>
Hi,
I have this code but I have to now make the code add a new id as this existing code is cloning the row with the same id. Then I have to appendChild(clone with new id) AFTER the last <tr> tag.
Thank you,
vkimura
I have this code but I have to now make the code add a new id as this existing code is cloning the row with the same id. Then I have to appendChild(clone with new id) AFTER the last <tr> tag.
Thank you,
vkimura
<table border="1">
<tr><td><input type="button" value="delete row" onclick="deleteRow()" /></td></tr>
<tr><td><input type="button" value="append row" onclick="copyAppendRow()" /></td></tr>
<tr id="row1"><td>field 1</td></tr>
<tr id="row2"><td>field 2</td></tr>
</table>
<script type="text/javascript">
/*delete first row:*/
function deleteRow() {
document.getElementById("row1").parentNode.removeChild(row1);
}
function copyAppendRow() {
/*clone Element:*/
var row2El=document.getElementById("row2");
var clone=row2El.cloneNode(true);
clone.id="row3";
/*get Element with id row2, clone Content into variable 'clone'*/
/*true or false refers to cloning the content*/
/*now do whatever you want with the cloned element*/
row2El.appendChild(clone);
/*in this case we are appending it to the row2 Element*/
}
</script>
ASKER
Hi mplungjan,
Thank you so much for this insight and code. Works well. Is there an easy way to change the id and increment it. So the next id is "row3", "row4", "row5", etc.
Thank you,
vkimura
Thank you so much for this insight and code. Works well. Is there an easy way to change the id and increment it. So the next id is "row3", "row4", "row5", etc.
Thank you,
vkimura
Sure
But do you want to add the row after a given row, or always to the end?
But do you want to add the row after a given row, or always to the end?
<table border="1" id="t1">
<tr><td><input type="button" value="delete row" onclick="deleteRow()" /></td></tr>
<tr><td><input type="button" value="append row" onclick="copyAppendRow()" /></td></tr>
<tr id="row1"><td>field 1</td></tr>
<tr id="row2"><td>field 2</td></tr>
</table>
<script type="text/javascript">
/*delete first row:*/
function deleteRow() {
document.getElementById("row1").parentNode.removeChild(row1);
}
function copyAppendRow() {
var table = document.getElementById("t1");
var lastRow = table.rows[table.rows.length-1]; // last row
/*clone Element:*/
/*get row, clone Content into variable 'clone'*/
var clone=lastRow.cloneNode(true);
/*true or false refers to cloning the content*/
// clone.id="row"+(table.rows.length-1); // call it rowX where X is the current number of actual rows
// get the ID from the row instead of using the number of rows
var num = (lastRow.id.indexOf('row')!=-1)?parseInt(lastRow.id.replace('row','')):1; // perhaps we deleted all rows
var id = "row"+(num+1);
clone.id=id;
/*now do whatever you want with the cloned element*/
/*in this case we are appending it to the TBODY */
lastRow.parentNode.appendChild(clone);
alert(table.innerHTML); // show what we did
}
</script>
ASKER
Hi mplungjan,
Wow, that's great. I suppose it would be nice to be able to add the row to the end or after or before. Is it easy to show me how?
Thank you so much for your help. You're really a genius at Javascript. It's something I'm beginning to learn.
Cordially,
vkimura
Wow, that's great. I suppose it would be nice to be able to add the row to the end or after or before. Is it easy to show me how?
Thank you so much for your help. You're really a genius at Javascript. It's something I'm beginning to learn.
Cordially,
vkimura
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.
You want to append the row to the tbody
This code was tested in IE7 and FF3
Open in new window