Shaon Shaon - 29 days ago 8
HTML Question

How to addition two row's values in one single column using jquery?

code in ideone

I have a row name roti. there has 12 distinct column. First 7 column takes different values and add them. Then the 8 column named TOTAL SALE (AMOUNT) is the summation of first 7 column with two rows. I gave the full code below. I actually manage to calculate first 6 column. But in 7 column I have faced the problem. it didn't calculate the actual result that I want. It adds extra 0.

Second row value doesn't add up with the 8th column

How can I be able to add 4p@14 row values in total sale (amount) column?

Actual calculation given below in

<script> </script>
tag

<!DOCTYPE html>
<head>
<title>Tortilla Daily Sell 1</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!--for online also works on ofline-->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</head>
<body>
<table border="1">
<tr>
<th>Product Title</th>
<th>PIECE and RATE</th>
<td>DISTRIB. PACKET</td>
<td>(BONUS) (PACKET)</td>
<td>(REPLACE) (PACKET)</td>
<td>(RETURN)(PACKET)</td>
<td>TOTAL SALE (PACKET)</td>
<td>SALES IN AMOUNT</td>
<td>EXT. SALE (PACKET)</td>
<td>TOTAL SALE (AMOUNT)</td>
<td>DUE RECEIVED</td>
<td>(DUE IN AMOUNT)</td>
<td>(EXP. & COM.)</td>
<td>TOTAL RECEIVED</td>
</tr>
<!-- ROTI -->
<tr class="product-row">
<th rowspan="2">ROTI</th>
<td>6p@20</td>
<td>
<input type="text"
class="form-control distrb-pak" id="distributedPacketForRoti6p20tk"
name='distributedPacketForRoti6p20tk'
value="0">

<input type="hidden" class="price" name="price" value="20">
</td>
<td>
<input type="text"
class="form-control bonus-pak" id="bonusForRoti6p20tk" name='bonusForRoti6p20tk'
value="0">
</td>
<td><input type="text"
class="form-control replace-pak" id="replaceForRoti6p20tk" name='replaceForRoti6p20tk'
value="0">
</td>
<td>
<input type="text"
class="form-control return-pak" id="returnForRoti6p20tk" name='returnForRoti6p20tk'
value="0">
</td>

<td>
<input type="text"
class="form-control total-sale-pak" id="totalSalePacketForRoti6p20tk"
name='totalSalePacketForRoti6p20tk'
value="0" readonly>

</td>

<td>
<input type="text"
class="form-control sales-in-amount" id="salesInAmountForRoti6p20tk"
name='salesInAmountForRoti6p20tk'
value="0" readonly>
</td>
<td>
<input type="text"
class="form-control extSale" id="extSaleForRoti6p20tk" name='extSaleForRoti6p20tk'
value="0">
</td>
<td rowspan="2">
<input type="text"
class="form-control totalSale" id="totalSaleAmountForRoti"
name='totalSaleAmountForRoti'
value="0" readonly>
</td>

<td rowspan="2">

<input type="text"
class="form-control dueReceived" id="dueReceivedForRoti" name='dueReceivedForRoti'
value="0">

</td>

<td rowspan="2">

<input type="text"
class="form-control dueAmount" id="dueInAmountForRoti" name='dueInAmountForRoti'
value="0">

</td>
<td rowspan="2">

<input type="text"
class="form-control expAndCom" id="expAndComForRoti" name='expAndComForRoti'
value="0">

</td>
<td rowspan="2">
<input type="text"
class="form-control totalReceived" id="totalReceivedForRoti" name='totalReceivedForRoti'
value="0" readonly>

</td>

</tr>


<!-- ROTI -->
<tr class="product-row">
<td>4p@14</td>
<td>
<input type="text"
class="form-control distrb-pak" name='distributedPacketForRoti4p14tk'
id="distributedPacketForRoti4p14tk" value="0">
<input type="hidden" class="price" name="price" value="14">
</td>
<td>
<input type="text"
class="form-control bonus-pak" name='bonusForRoti4p14tk'
id="bonusForRoti4p14tk" value="0">
</td>
<td><input type="text"
class="form-control replace-pak" name='replaceForRoti4p14tk'
id="replaceForRoti4p14tk" value="0">
</td>
<td>
<input type="text"
class="form-control return-pak" name='returnForRoti4p14tk'
id="returnForRoti4p14tk" value="0">
</td>
<td>
<input type="text"
class="form-control total-sale-pak" name='totalSalePacketForRoti4p14tk'
id="totalSalePacketForRoti4p14tk" value="0" readonly>

</td>

<td>
<input type="text"
class="form-control sales-in-amount" name='salesInAmountForRoti4p14tk'
id="salesInAmountForRoti4p14tk" value="0" readonly>

</td>
<td>
<input type="text"
class="form-control extSale" id="extSalesForRoti4p14tk"
name='extSalesForRoti4p14tk'
value="0">
</td>


</tr>
<script>

$('.distrb-pak, .bonus-pak, .replace-pak, .return-pak, .dueReceived, .dueAmount, .extSale, .expAndCom').on('keydown keyup', function () {

var distPakCount = $(this).parents('.product-row').find('.distrb-pak').val();
var bonusPakCount = $(this).parents('.product-row').find('.bonus-pak').val();
var replacePakCount = $(this).parents('.product-row').find('.replace-pak').val();
var returnPakCount = $(this).parents('.product-row').find('.return-pak').val();
var totalSalePak = $(this).parents('.product-row').find('.total-sale-pak');
var salesInAmount = $(this).parents('.product-row').find('.sales-in-amount');
var extSale = $(this).parents('.product-row').find('.extSale').val();
var totalSale = $(this).parents('.product-row').find('.totalSale');
var dueReceived = $(this).parents('.product-row').find('.dueReceived').val();
var dueAmount = $(this).parents('.product-row').find('.dueAmount').val();
var totalReceived = $(this).parents('.product-row').find('.totalReceived');
var expCom = $(this).parents('.product-row').find('.expAndCom').val();
var price = $(this).parents('.product-row').find('.price').val();


var totalSalePakCount = parseInt(distPakCount) - parseInt(bonusPakCount) -
parseInt(returnPakCount) - parseInt(replacePakCount);
totalSalePak.val(totalSalePakCount);

var salesInAmount_ = totalSalePakCount * parseInt(price);
salesInAmount.val(salesInAmount_);

var totalSaleInAmount = salesInAmount_ + parseInt(extSale);
totalSale.val(totalSaleInAmount);

var totalReceivedAmount = parseInt(totalSaleInAmount) + parseInt(dueReceived) - parseInt(dueAmount)
- parseInt(expCom);

totalReceived.val(totalReceivedAmount);
//totalSale
});


</script>


</body>


This is the output. It adds extra 0 in total sale column and did not occur anything when I put value in row 2

enter image description here

Answer

Loop over all the .totalReceived values and add their values. Then put this into .totalSale:

var totalSale = 0;
$(".totalReceived").each(function() {
    totalSale += parseInt($(this).val());
}
$(".totalSale").val(totalSale);