
Originally Posted by
jscheuer1
The code I wrote shouldn't even look at row 0 for id changing:
Code:
if(els[i] == t.rows[c + 1]){ // we are already at least to row 1
++c;
if (els[i].id != 'dynamicFormWritePart')
els[i].id = '';
else break;
}
<tr id="dynamicFormReadPart"> is row 0. But if I've miscalculated or if you've put a header row in there later:
There was always a header row, I probably just forgot to include it in my example.
Code:
<table id="partFields" summary="Parts associated with this order request">
<tbody>
<tr>
<th>Qty</th>
<th>Part Number</th>
<th>Part Description</th>
<th>Serial</th>
</tr>
<tr id="dynamicFormReadPart">
<td><input name="partQuantity0" value="" size="5" type="text"></td>
<td><input name="partNumber0" value="" type="text"></td>
<td><input name="partDescription0" value="" size="50" type="text"></td>
<td><input name="partSerial0" value="" type="text"></td>
</tr>
<tr id="dynamicFormWritePart"><!-- USED AS PLACEHOLDER TO INSERT DYNAMIC FIELDS --></tr>
</tbody>
</table>
Code:
if(c > 1 && els[i].id != 'dynamicFormWritePart')
I tried that, and I also substituted the c for an i neither of which worked
You could also skip having a dynamicFormWritePart id by simply (as long as their is a last row to insert before, otherwise you could simply append):
Well the only purpose of that last row is for the insert, however the method you suggested is still more elegant then the one I was proposing so I updated it accordingly.
below is the JS file as it reads right now. The html table hasnt changed from my last example
Code:
function addOrderFormFields(dynamicFormReadId, dynamicFormWriteId)
{
var newFields = document.getElementById(dynamicFormReadId).cloneNode(true);
var t = document.getElementById('partFields');
/* insert the cloned node */
t.rows[t.rows.length - 1].parentNode.insertBefore(newFields, t.rows[t.rows.length - 1]);
/* re-assign the input name attributes */
for(var c = 0, els = t.getElementsByTagName('*'), i = 0; i < els.length; ++i)
{
if(els[i] == t.rows[c + 1])
{
++c;
if(c>1 && els[i].id != 'dynamicFormWritePart')
els[i].id = '';
else
break;
}
else if(/^part/.test(els[i].name))
els[i].name = els[i].name.replace(/\d+/, '') + c;
}
}
Bookmarks