This is a shortened version of my problem. Below I am cloning the first
data row and appending it to create a new row. If you make selections/add
values and then press Add Row, the text box value is carried to the new
row's text element also (the select doesn't retain it's selected value).
How can I create a new row with the form elements in the original no value
state? I tried cloning the Node on page load, but then it only allows me to
add one row. Is there a way to clone the row and retain it to add as many
rows as I like. I know I can clear all the values of the row after I create
it, but that seems like a long way around especially since I have many
fields in my actual code.
Any pointers appreciated,
John
<form name="myForm" method="post">
<input type="button" value="Add Row" onclick="addRow ()">
<table border="1" cellspacing="0" cellpadding="0" >
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
<tbody id="datatable" >
<tr>
<td>
<select name="mySelect" >
<option>Selec t</option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>
</td>
<td>
<input type="text" name="myTextfie ld">
</td>
</tr>
</tbody>
</table>
</form>
<script language="JavaS cript">
function addRow(){
var myTbody=documen t.getElementByI d('datatable');
newRow=myTbody. firstChild.clon eNode(true);
myTbody.appendC hild(newRow);
}
</script>
data row and appending it to create a new row. If you make selections/add
values and then press Add Row, the text box value is carried to the new
row's text element also (the select doesn't retain it's selected value).
How can I create a new row with the form elements in the original no value
state? I tried cloning the Node on page load, but then it only allows me to
add one row. Is there a way to clone the row and retain it to add as many
rows as I like. I know I can clear all the values of the row after I create
it, but that seems like a long way around especially since I have many
fields in my actual code.
Any pointers appreciated,
John
<form name="myForm" method="post">
<input type="button" value="Add Row" onclick="addRow ()">
<table border="1" cellspacing="0" cellpadding="0" >
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
<tbody id="datatable" >
<tr>
<td>
<select name="mySelect" >
<option>Selec t</option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>
</td>
<td>
<input type="text" name="myTextfie ld">
</td>
</tr>
</tbody>
</table>
</form>
<script language="JavaS cript">
function addRow(){
var myTbody=documen t.getElementByI d('datatable');
newRow=myTbody. firstChild.clon eNode(true);
myTbody.appendC hild(newRow);
}
</script>
Comment