I have a simple function that computes the difference between two text fields: txtItemAmount1 and txtAdjustmentAmount. The difference it placed in a label (Difference).
The problem I'm having is that by clicking the "Add Row" button, the user can add more "txtItemAmount" rows. I would like all of these rows to be included in the calculation. For example, right now if the user enters "20.00" in txtAdjustmentAmount, and "10.00" in txtItemAmount1, the difference label will show "10.00." If I click the "Add row" button, and type in "1.00" I would like that also added to this calculation, thus the difference would be "11." Can someone provide a code example of how to incorporate these newly added fields in the calculation?
Thanks!
<body>
<form name="frmInput" id="frmInput">
<table>
<tr>
<td><p><label for="AdjustmentAmount">Adjustment Amount:</label></p></td>
<td><p><input name="txtAdjustmentAmount" type="text" id="txtAdjustmentAmount" size="10" maxlength="10"></p></td>
</tr>
</table>
<table>
<tr>
<td><p><strong><font color="#FF0000">Difference:<label for="Difference" id="Difference"></label></font></strong></td>
</tr>
</table>
<table id="tblDetail">
<tbody>
<tr>
<td colspan="6">Item Amount:</td>
</tr>
<tr>
<td colspan="6">
<input type="button" class="button" value= "Add Row" id="btnNewRow" name="btnNewRow" onClick="javascript:addNewRow();">
<input type="hidden" id="txtIndex" name="txtIndex" value="1">
</td>
</tr>
<tr>
<td>
<p>
<input name="txtItemAmount1" type="text" id="txtItemAmount1" size="10" maxlength="10"
onKeyUp="calcDifference(frmInput.txtItemAmount1.value, frmInput.txtAdjustmentAmount.value)">
</p>
</td>
</tr>
</tbody>
</table>
</form>
<script language="javascript">
function addNewRow()
{
var iX = document.getElementById("txtIndex").value;
iX ++;
document.getElementById("txtIndex").value = iX;
var tbl = document.getElementById("tblDetail").getElementsByTagName("TBODY")[0];
var tr = document.createElement("TR");
tbl.appendChild(tr);
//txtItemAmount1
var tdItemAmount = document.createElement("TD");
tr.appendChild(tdItemAmount);
var p = document.createElement("P");
tdItemAmount.appendChild(p);
var txtItemAmount = document.createElement("input");
p.appendChild(txtItemAmount);
txtItemAmount.id = "txtItemAmount" + iX;
txtItemAmount.setAttribute('name','txtItemAmount' + iX);
txtItemAmount.setAttribute('size',10);
}
function calcDifference(txtAdjustmentAmount, txtItemAmount)
{
var txtAdjustmentAmount = parseFloat(document.getElementById('txtAdjustmentAmount').value);
var txtItemAmount = parseFloat(document.getElementById('txtItemAmount1').value);
var Difference = txtItemAmount - txtAdjustmentAmount;
document.getElementById('Difference').innerHTML = Difference
}
</script>
</body>