Am trying to do multiple simple calculations using the jAutoCalc.js jquery plugin in a form where the rows are dynamically added/removed. Please visit this link to see the live demo. While the calculation works for the first row it is not happening for the subsequent rows. Also I like to sum all the total values using the same plugin and stick it in the 'Sum of Total' input box at the bottom.Thanks in advance for helping me out.
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
// Write JavaScript here
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if (rowCount < 4) { // limit the user from creating fields more than your limits
var row = table.insertRow(rowCount);
//var row = table.insertRow(rowCount-1);
var colCount = table.rows[0].cells.length;
row.id = 'row_'+rowCount;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.outerHTML = table.rows[0].cells[i].outerHTML;
}
var listitems= row.getElementsByTagName("input")
for (i=0; i<listitems.length; i++) {
listitems[i].setAttribute("oninput", "calculate('"+row.id+"')");
}
} else {
alert("Maximum Passenger per ticket is 4.");
}
}
function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if (null !== chkbox && true === chkbox.checked) {
if (rowCount <= 1) { // limit the user from removing all the fields
alert("Cannot Remove all the Records.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
</script>
<script type="text/javascript" src="http://rawgit.com/c17r/jAutoCalc/master/jAutoCalc.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function() {
$('form[name=cart] tr[name=line_items]').jAutoCalc({keyEventsFire: true, decimalPlaces: 2});
$('form[name=cart]').jAutoCalc({decimalPlaces: 2});
});
//-->
</script>
<body>
<form name="cart">
<input type="button" value="Add" onClick="addRow('dataTable')" />
<input type="button" value="Remove" onClick="deleteRow('dataTable')" />
<table id="dataTable" class="form" border="1">
<tbody>
<tr name="line_items" id='row_0'>
<p>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td>
<label>Qty</label>
<input type="text" size="1" required="required" class="qty" name="qty" >
</td>
<td>
<label for="cuprice">Price</label>
<input type="text" size="3" required="required" class="price" name="price" >
</td>
<td>
<label for="ctp">Total</label>
<!--<td><input type="text" size="3" readonly class="total" name="total" id="total" /></td>-->
<td><input type="text" name="item_total" value="" jAutoCalc="{qty} * {price}"></td>
</td>
</p>
</tr>
</tbody>
</table>
</form>
Sum of Total:<input type="text" name="sum"/>
</body>
Plugin Link: https://github.com/c17r/jAutoCalc
Now using 'Calx' jQuery Plugin to do the calculation on a form where the rows are created dynamically.