I want to auto sum the rows in html and then display the result by using jQuery I tried but I failed, bellow are my sample codes which are using loop
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" /></script>
<script>
$.fn.fonkTopla = function() {
var toplam = 1;
this.each(function() {
var deger = fonkDeger($(this).val());
//get the data attr value
var diff = $(this).data('diff');
var Quantity_val = $(this).closest('tr').find('.Quantity').val();
var QuantityBack_val = $(this).closest('tr').find('.QuantityBack').val();
var UnitPrice_val = $(this).closest('tr').find('.UnitPrice').val();
var paid_val = $(this).closest('tr').find('.paid').val();
//On based on data attribute, if the input field is paid, do not multiply,
if(!diff){
//toplam *= deger;
toplam= (Quantity_val * UnitPrice_val);
//take the result minus paid field
total = (toplam) - paid_val-(QuantityBack_val * UnitPrice_val);
}
});
return total;
};
function fonkDeger(veri) {
return (veri != '') ? parseInt(veri) : 1;
}
$(document).ready(function(){
$('input[name^="fiyat"]').bind('keyup', function() {
//for multiple tr, get the closest tr field value to calculate and display
$(this).closest('tr').find('.toplam').html( $(this).closest('tr').find('input[name^="fiyat"]').fonkTopla());
});
});
</script>
</head>
<body>
<?php
for($counter1=0;$counter1<=2;$counter1++)
{
?>
<table>
<?php
for($counter2=0;$counter2<=2;$counter2++)
{
?>
<tr>
<td><input type="number" name="fiyat[]" class="Quantity" /></td>
<td><input type="number" name="fiyat[]" data-diff="true" class="QuantityBack" /></td>
<td><input type="number" name="fiyat[]" class="title UnitPrice" value="500" /></td>
<td><input type="number" name="fiyat[]" data-diff="true" class="paid" /></td>
<td style="text-align:right" bgcolor="#FF5733"><span class="toplam"></span>
</tr>
<?php
}
?>
</table>
<?php
}
?>
<!--I NEED TO CALCULATE SUB TOTAL OF EACH TABLE IN LOOP IN BELOW CODES, I NEED SUB TOTAL UNDER LAST RED COLUMN-->
<script>
var totalRow = '', columnNo = $('table tr:first td').length;
for (var index = 0; index < columnNo; index++) {
var total = 0;
$('table tr').each(function () {
total += +$('td', this).eq(index).text(); //+ will convert string to number
});
totalRow += '<td>' + total + '</td>';
}
$('table').append('<tr>' + totalRow + '</tr>');
</script>
<!--end of jQuery used to auto sum subtotal-->
</body>
</html>
The problem is in jQuery which makes Total (that jQuery is located at bottom of the codes) Please anyone can help us to accomplish this task. I need only the sub total of each table in loop but in red column. Thank you
calc_total()