Hello,
I am working on a form in whcih i have to dynamically add fields on the form. I got that working using javascript but now when I am trying to tidy up the form a bit then I am in trouble as I want to use table to put the fields in and when user adds another row then it should come in between that table. How to do it ?
Here’s my code:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
var counter = 0;
window.onload = function ()
{
document.getElementById('moreFields').onclick = moreFields;
}
function moreFields()
{
counter++;
var newFields = document.getElementById('readroot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
</script>
</head>
<body>
<table width="965" border="1" cellpadding="5" cellspacing="0" align="center" style="border-collapse: collapse">
<tr style="background-color:#000; color:#fff;">
<td width="35">#</td>
<td width="321">What</td>
<td width="95">Icon</td>
<td width="202">How</td>
<td width="202">Why</td>
<td>Act.</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td>
<form style="margin:0px;" name="frmForm" id="frmForm" method="post" action="formtest.php">
<input name="num[]" value="" size="3" />
<input name="what[]" value="" size="50" />
<select name="icon[]">
<option>Knack</option>
<option>Quality</option>
<option>Safety</option>
<option>Enviornment</option>
<option>Critical</option>
</select>
<input name="how[]" value="" size="30" />
<input name="why[]" value="" size="30" />
<input type="button" style="width:50px;" id="moreFields" onclick="moreFields();" value="ADD" />
<span id="writeroot"></span>
<p align="center"><input type="button" onclick="frmForm.submit();" value="Submit" /></p>
</form>
</td>
</tr>
</table>
<div id="readroot" style="display:none;">
<input name="num[]" value="" size="3" />
<input name="what[]" value="" size="50" />
<select name="icon[]">
<option>Knack</option>
<option>Quality</option>
<option>Safety</option>
<option>Enviornment</option>
<option>Critical</option>
</select>
<input name="how[]" value="" size="30" />
<input name="why[]" value="" size="30" />
<input type="button" style="width:50px;" value="DEL" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
</tr>
</div>
</body>
</html>
Please help.
Thanks.