Javascript- How do i get this script to display the calculated total? - javascript

I am aware of the other errors in this script but i do know how to fix those, the main problem is that i cannot figure out how to fix the calculated total function at the bottom of this script. I am fairly new to this however after checking several guides i could not find anything that would help.
<html>
<head>
</head>
<body>
<table>
<tr>
<td class="ColHdr">ID</td>
<td class="ColHdr">Product</td>
<td class="ColHdr">Price</td>
<td class="ColHdr">Discounted Price</td>
<td class="ColHdr">Quantity</td>
</tr>
<tr>
<td>1</td>
<td class="Product">Hat</td>
<td class="Price" id="Price01">19.99</td>
<td class="Price" id="DiscountedPrice01"></td>
<td><input id="Quantity01" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>2</td>
<td class="Product">Shirt</td>
<td class="Price" id="Price02">27.99</td>
<td class="Price" id="DiscountedPrice02"></td>
<td><input id="Quantity02" type="number" value="0" `enter code here`onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>3</td>
<td class="Product">Jersey</td>
<td class="Price" id="Price03">109.99</td>
<td class="Price" id="DiscountedPrice03"></td>
<td><input id="Quantity03" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>4</td>
<td class="Product">Socks</td>
<td class="Price" id="Price04">4.49</td>
<td class="Price" id="DiscountedPrice04"></td>
<td><input id="Quantity04" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>5</td>
<td class="Product">Sweatshirt</td>
<td class="Price" id="Price05">39.79</td>
<td class="Price" id="DiscountedPrice05"></td>
<td><input id="Quantity05" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>6</td>
<td class="Product">Bumper Sticker</td>
<td class="Price" id="Price06">3.29</td>
<td class="Price" id="DiscountedPrice06"></td>
<td><input id="Quantity06" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>7</td>
<td class="Product">Jacket</td>
<td class="Price" id="Price07">179.99</td>
<td class="Price" id="DiscountedPrice07"></td>
<td><input id="Quantity07" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>8</td>
<td class="Product">Sun Glasses</td>
<td class="Price" id="Price08">12.99</td>
<td class="Price" id="DiscountedPrice08"></td>
<td><input id="Quantity08" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>9</td>
<td class="Product">Poster</td>
<td class="Price" id="Price09">8.99</td>
<td class="Price" id="DiscountedPrice09"></td>
<td><input id="Quantity09" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>10</td>
<td class="Product">Banner</td>
<td class="Price" id="Price10">5.49</td>
<td class="Price" id="DiscountedPrice10"></td>
<td><input id="Quantity10" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>11</td>
<td class="Product">Ski Hat</td>
<td class="Price" id="Price11">15.09</td>
<td class="Price" id="DiscountedPrice11"></td>
<td><input id="Quantity11" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>12</td>
<td class="Product">Tee Shirt</td>
<td class="Price" id="Price12">14.99</td>
<td class="Price" id="DiscountedPrice12"></td>
<td><input id="Quantity12" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>13</td>
<td class="Product">Tank Top</td>
<td class="Price" id="Price13">9.99</td>
<td class="Price" id="DiscountedPrice13"></td>
<td><input id="Quantity13" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>14</td>
<td class="Product">Pin</td>
<td class="Price" id="Price14">1.79</td>
<td class="Price" id="DiscountedPrice14"></td>
<td><input id="Quantity14" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>15</td>
<td class="Product">Framed Photo</td>
<td class="Price" id="Price15">49.99</td>
<td class="Price" id="DiscountedPrice15"></td>
<td><input id="Quantity15" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>16</td>
<td class="Product">Team License Plate</td>
<td class="Price" id="Price16">17.99</td>
<td class="Price" id="DiscountedPrice16"></td>
<td><input id="Quantity16" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>17</td>
<td class="Product">Team Pen</td>
<td class="Price" id="Price17">3.99</td>
<td class="Price" id="DiscountedPrice17"></td>
<td><input id="Quantity17" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>18</td>
<td class="Product">Decorative Baseball</td>
<td class="Price" id="Price18">11.99</td>
<td class="Price" id="DiscountedPrice18"></td>
<td><input id="Quantity18" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>19</td>
<td class="Product">Decorative Football</td>
<td class="Price" id="Price19">29.99</td>
<td class="Price" id="DiscountedPrice19"></td>
<td><input id="Quantity19" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>20</td>
<td class="Product">Decorative Basketball</td>
<td class="Price" id="Price20">24.99</td>
<td class="Price" id="DiscountedPrice20"></td>
<td><input id="Quantity20" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>21</td>
<td class="Product">Window Decal</td>
<td class="Price" id="Price21">5.99</td>
<td class="Price" id="DiscountedPrice21"></td>
<td><input id="Quantity21" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td>22</td>
<td class="Product">Window Decal (Team Set)</td>
<td class="Price" id="Price22">19.99</td>
<td class="Price" id="DiscountedPrice22"></td>
<td><input id="Quantity22" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><br><input type="button" id="CalculateTotal" value="Calculate Total" onclick="CalculcateTotal();"></td>
<td></td>
<td id="TotalLabel"><br>Total:</td>
<td id="Total"><br></td>
</tr>
</table>
<br>
<p>
Plan:
<input type="radio" value="1" name="Plan" checked="checked" onclick="CalcDiscountedPrice(this.value, this.id);">Standard
<input type="radio" value=".95" name="Plan" onclick="CalcDiscountedPrice(this.value, this.id);">Silver
<input type="radio" value=".85" name="Plan" onclick="CalcDiscountedPrice(this.value, this.id);">Gold
</p>
<img src="AcmeLogo.jpg" onload="CalcDiscountedPrice(1);">
<body>
<script type="text/javascript" >
function CalcDiscountedPrice(PercentToPay) {
var i = 1;
for (i = 1; i <= 22; i++) {
if (i < 10) {
var CellNumber = "0" + i;
} else {
var CellNumber = i;
}
var DiscountedCellId = "DiscountedPrice" + CellNumber;
var DiscountedCell = document.getElementById(DiscountedCellId);
var PriceCellId = "Price" + CellNumber;
var PriceCell = document.getElementById(PriceCellId);
var Price = Number(PriceCell.innerHTML);
var DiscountedPrice = Math.round((Price * PercentToPay) * 100)/100;
DiscountedCell.innerHTML = DiscountedPrice;
DiscountedCell.style.color = "#ee0c00";
DiscountedCell.style.fontWeight = "bold";
DiscountedCell.style.fontStyle = "italic";
}
}
function CalculateTotal() {
var total= O;
var i = 1;
for (var i = 1; b <= 22; i++) {
if (i < 10) {
var CellNumber = "0" + i;
} else {
var CellNumber = i;
}
var DiscountedCellId = "DiscountedPrice" + CellNumber;
var DiscountedCell = document.getElementById(DiscountedCellId);
}
}
</script>
</html>

Amended below is your javascript. Note, I have uncapitalised all your variables as that is bad practice.
function CalcDiscountedPrice(percentToPay) {
var i = 1;
for (i = 1; i <= 22; i++) {
if (i < 10) {
var cellNumber = "0" + i;
} else {
var cellNumber = i;
}
var discountedCellId = "DiscountedPrice" + cellNumber;
var discountedCell = document.getElementById(discountedCellId);
var priceCellId = "Price" + cellNumber;
var priceCell = document.getElementById(priceCellId);
var price = Number(priceCell.innerHTML);
var discountedPrice = Math.round((price * percentToPay) * 100)/100;
discountedCell.innerHTML = discountedPrice;
discountedCell.style.color = "#ee0c00";
discountedCell.style.fontWeight = "bold";
discountedCell.style.fontStyle = "italic";
}
CalculateTotal();
}
function CalculateTotal() {
var total= 0;
var i = 1;
for (var i = 1; i <= 22; i++) {
if (i < 10) {
var cellNumber = "0" + i;
} else {
var cellNumber = i;
}
var discountedCellId = "DiscountedPrice" + cellNumber;
var discountedCellPrice = document.getElementById(discountedCellId).innerHTML;
var price = discountedCellPrice;
if(discountedCellPrice === "") { // check if a discount is applied, use normal price if not
var normalCellId = "Price" + cellNumber;
price = document.getElementById(normalCellId).innerHTML;
}
var quantityCellId = "Quantity" + cellNumber;
total += price * document.getElementById(quantityCellId).value;
}
document.getElementById("Total").innerHTML = total;
}
And as #RamblinRose pointed out, you haven't called CalculateTotal correctly

You have a typo, you're calling
onclick="CalculcateTotal();"
but your function is
function CalculateTotal() {
Change your onclick to
onclick="CalculateTotal();"
Try F-12. There's a debugger in that browser.

Related

how to get the sum of all row value in jquery

I'm trying to achieve two additions in my code.
I am trying to get the sum of the row values in the last column of the row
I am trying to get the sum of all the final columns in another div
I've achieved the first, my code gives me the correct value of each row. However the second sum is not populating the addition. It's giving me the same value of each row, but I want the value of all rows.
$('.bonus-sum').keyup(function() {
var sum = 0;
var salary = parseFloat($(this).closest('tr').find('.wagein').text());
var bonus = parseFloat($(this).closest('tr').find('.bonus-sum').val()) || 0;
$(this).closest('tr').find('.bonus-in:checked').each(function() {
sum = salary + bonus;
});
$(this).closest('tr').find('.netpay').text(sum.toFixed(2));
var netpay = 0;
netpay += sum;
$('#total').text('₹ ' + netpay.toFixed(2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<table>
<thead>
<tr>
<th class="text-center"><input type="checkbox" checked="checked" class="checkAll" name="checkAll" /></th>
<th>#</th>
<th>Beneficiary Name</th>
<th class="text-right box">Bonus ₹</th>
<th class="text-right">Salary ₹</th>
<th class="text-right">Net pay ₹</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>1</td>
<td>Chellammal Kochimoni</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>2</td>
<td>Christal Prema G.</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>3</td>
<td>Kamalesan T.</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>4</td>
<td>Palammal A.</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>4</td>
<td>Thangapazham</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
</tbody>
</table>
</div>
<div>Total Net Pay <span id="total">₹ 0.00</span></div>
To fix this you need to create a loop which iterates through all the .netpay elements, not just the one which was updated, and generates the total value.
In addition, you need to perform the same action when the checkbox is changed. The logic itself can also be made more succinct and DRY, like this:
$('.bonus-sum').on('input', updateRowTotal);
$('.bonus-in').on('change', updateRowTotal).trigger('change'); // trigger is to set values on load
function updateRowTotal() {
let $tr = $(this).closest('tr');
let salary = parseFloat($tr.find('.wagein').text()) || 0;
let bonus = parseFloat($tr.find('.bonus-sum').val()) || 0
let rowTotal = salary + ($tr.find('.bonus-in:checked').length ? bonus : 0);
$tr.find('.netpay').text(rowTotal.toFixed(2));
updateTotal();
}
function updateTotal() {
let total = 0;
$('.netpay').each((i, el) => total += parseFloat(el.textContent.trim() || 0));
$('#total').text('₹ ' + total.toFixed(2));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<table>
<thead>
<tr>
<th class="text-center"><input type="checkbox" checked="checked" class="checkAll" name="checkAll" /></th>
<th>#</th>
<th>Beneficiary Name</th>
<th class="text-right box">Bonus ₹</th>
<th class="text-right">Salary ₹</th>
<th class="text-right">Net pay ₹</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>1</td>
<td>Chellammal Kochimoni</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>2</td>
<td>Christal Prema G.</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>3</td>
<td>Kamalesan T.</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>4</td>
<td>Palammal A.</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>4</td>
<td>Thangapazham</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
</tbody>
</table>
</div>
<div>Total Net Pay <span id="total">₹ 0.00</span></div>
Note the removal of all the duplicate id attributes in your HTML . They are invalid, and are not required anyway.
I have introduced new variable totalsum.
Replace the below function .your code will work as expected
<script>
var totalsum=0;
$('.bonus-sum').keyup(function() {
var sum = 0;
var salary = parseFloat($(this).closest('tr').find('.wagein').text());
var bonus = parseFloat($(this).closest('tr').find('.bonus-sum').val()) || 0;
$(this).closest('tr').find('.bonus-in:checked').each(function() {
sum = salary + bonus;
totalsum+=sum;
});
$(this).closest('tr').find('.netpay').text(sum.toFixed(2));
var netpay = 0;
netpay += totalsum;
$('#total').text('₹ ' + netpay.toFixed(2));
});
</script>

Get total from multiple class onchange

Goodday, i'm trying to get total and grandtotal from textinput . First please check my script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>No</td>
<td>Item</td>
<td>Qty</td>
<td>est</td>
<td>tax</td>
<td>Total</td>
<td>GrandTotal</td>
</tr>
<tr>
<td>No</td>
<td>ITEM 1</td>
<td>2</td>
<td><input type="text" class="est"></td>
<td><input type="text" class="tax"></td>
<td><input type="text" readonly class="total"></td>
<td><input type="text" readonly class="grandtotal"></td>
</tr>
<tr>
<td>No</td>
<td>ITEM 1</td>
<td>2</td>
<td><input type="text" class="est"></td>
<td><input type="text" class="tax"></td>
<td><input type="text" readonly class="total"></td>
<td><input type="text" readonly class="grandtotal"></td>
</tr>
</table>
As you can i see, there is .total and .grandtotal. I want to get it with this
total = .est * .qty
grandtotal = .total * .tax
So, i want to onchange in two fields .est & .tax .
Maybe i can do this
$(document).on("change",".tax",function(){
//Code
});
and
$(document).on("change",".tax",function(){
//Code
});
But it will take time, so i'm trying to approach this way
$(document).on("change",".tax, .est",function(){
//I don't know to get value of .tax or .est only
});
How can i get the total and grandtotal ?
run and see result
$('tr').each(function() {
var qty = $($(this).children()[2]).text(),
$tax = $(this).find(".tax"),
$est = $(this).find(".est"),
$total = $(this).find(".total"),
$grandtotal = $(this).find(".grandtotal")
function onChange() {
var t = $est.val() * qty
var gt = $tax.val() * t
$total.val(t)
$grandtotal.val(gt)
}
$tax.change(onChange)
$est.change(onChange)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>No</td>
<td>Item</td>
<td>Qty</td>
<td>est</td>
<td>tax</td>
<td>Total</td>
<td>GrandTotal</td>
</tr>
<tr>
<td>No</td>
<td>ITEM 1</td>
<td>2</td>
<td><input type="text" class="est"></td>
<td><input type="text" class="tax"></td>
<td><input type="text" readonly class="total"></td>
<td><input type="text" readonly class="grandtotal"></td>
</tr>
<tr>
<td>No</td>
<td>ITEM 1</td>
<td>2</td>
<td><input type="text" class="est"></td>
<td><input type="text" class="tax"></td>
<td><input type="text" readonly class="total"></td>
<td><input type="text" readonly class="grandtotal"></td>
</tr>
</table>
I hope this help
$(function() {
var resultTax = 0, resultEst = 0, total = 0;
$(".tax").change( function() {
$(".tax").each(function() {
resultTax += parseFloat($(this).val())
});
$('.resultTax').text(resultTax);
$('.total').text($('.resultEst').text() + $('.resultTax').text());
});
$(".est").change( function() {
$(".est").each(function() {
resultEst += parseFloat($(this).val())
});
$('.resultEst').text(resultEst);
$('.total').text(parseFloat($('.resultEst').text()) + parseFloat($('.resultTax').text()));
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Tax <input type="text" class="tax" value="1">
Tax <input type="text" class="tax" value="2"><br>
Est <input type="text" class="est" value="3">
Est <input type="text" class="est" value="4"><br><br>
resultTax : <span class="resultTax"></span><br>
resultEst : <span class="resultEst"></span><br>
Total Tax + Est : <span class="total"></span>

How to sum Jquery Outputs

I created couple of rows and multiplied some data to get totals:
But I am not sure how to sum all the totals and print it in "Cash in Hand" row.
Following are my codes:
<script type="text/javascript">
$(document).ready(function() {
$('.row1').keyup(function(ev){
var row1c = $(this).val() * 1000;
$('.row1c').html((row1c).toFixed(2));
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$('.row2').keyup(function(ev){
var row2c = $(this).val() * 500;
$('.row2c').html((row2c).toFixed(2));
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$('.row3').keyup(function(ev){
var row3c = $(this).val() * 100;
$('.row3c').html((row3c).toFixed(2));
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$('.row4').keyup(function(ev){
var row4c = $(this).val() * 50;
$('.row4c').html((row4c).toFixed(2));
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$('.row5').keyup(function(ev){
var row5c = (row1c+row2c+row3c+row4c);
$('.row5c').html((row5c).toFixed(2));
});
});
</script>
<table border="2" cellpadding="5" cellspacing="2" align="center">
<h3 align="center">Cash Position as on...... </h3>
<tr>
<th>Note</th>
<th>Quantity</th>
<th>Total</th>
</tr>
<tr>
<td>1000 Tk Note</td>
<td><input type="text" name="pages" class="row1" /></td>
<td><span class="row1c">0.00</span></td>
</tr>
<tr>
<td>500 Tk Note</td>
<td><input type="text" name="pages" class="row2" /></td>
<td><span class="row2c">0.00</span></td>
</tr>
<tr>
<td>100 Tk Note</td>
<td><input type="text" name="pages" class="row3" /></td>
<td><span class="row3c">0.00</span></td>
</tr>
<tr>
<td>50 Tk Note</td>
<td><input type="text" name="pages" class="row4" /></td>
<td><span class="row4c">0.00</span></td>
</tr>
<tr>
<td colspan ="2">Cash In Hand (Sum of All Totals)</td>
<td><span class="row5c">0.00</span></td>
</tr>
</table>
All codes together here:
https://jsfiddle.net/rashelmiah/fb9opo36/1/
Could you please me find a way to sum all the totals?
Thanks in advance.
You need add the following code.
$('input[type=text]').keyup(function(){
var total = 0;
$('input[type=text]').each(function(){
total += Number($(this).parent().next().find('span').text());
})
$('.row5c').text(total);
})
Note: Your code had a lot of <script> tags which was unnecessary. And a lot of ready() function, which was unnecessary too. You can wrap the whole code inside one <script> tag and one ready() function.
Demo:
$(document).ready(function() {
$('.row1').keyup(function(ev){
var row1c = $(this).val() * 1000;
$('.row1c').html((row1c).toFixed(2));
});
$('.row2').keyup(function(ev){
var row2c = $(this).val() * 500;
$('.row2c').html((row2c).toFixed(2));
});
$('.row3').keyup(function(ev){
var row3c = $(this).val() * 100;
$('.row3c').html((row3c).toFixed(2));
});
$('.row4').keyup(function(ev){
var row4c = $(this).val() * 50;
$('.row4c').html((row4c).toFixed(2));
});
$('.row5').keyup(function(ev){
var row5c = (row1c+row2c+row3c+row4c);
$('.row5c').html((row5c).toFixed(2));
});
$('input[type=text]').keyup(function(){
var total = 0;
$('input[type=text]').each(function(){
total += Number($(this).parent().next().find('span').text());
})
$('.row5c').text(total.toFixed(2));
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="2" cellpadding="5" cellspacing="2" align="center">
<h3 align="center">Cash Position as on...... </h3>
<tr>
<th>Note</th>
<th>Quantity</th>
<th>Total</th>
</tr>
<tr>
<td>1000 Tk Note</td>
<td>
<input type="text" name="pages" class="row1" />
</td>
<td><span class="row1c">0.00</span>
</td>
</tr>
<tr>
<td>500 Tk Note</td>
<td>
<input type="text" name="pages" class="row2" />
</td>
<td><span class="row2c">0.00</span>
</td>
</tr>
<tr>
<td>100 Tk Note</td>
<td>
<input type="text" name="pages" class="row3" />
</td>
<td><span class="row3c">0.00</span>
</td>
</tr>
<tr>
<td>50 Tk Note</td>
<td>
<input type="text" name="pages" class="row4" />
</td>
<td><span class="row4c">0.00</span>
</td>
</tr>
<tr>
<td colspan="2">Cash In Hand (Sum of All Totals)</td>
<td><span class="row5c">0.00</span>
</td>
</tr>
</table>

Javascript multiply input field by a number value

I am trying to multiply the user input on input field "full_day" by the "var fullprice", and display the result in readonly input field "total_full" what am i doing wrong that is causing it not to work?
<div id="wrapper">
<div id="header"></div>
<table width="899" border="1" align="left" cellpadding="1">
<form action="" method="get" name="myform">
<tr>
<td width="275"><label>Company Name</label></td>
<td width="180"><input type="text" name="companyname" id="companyname" /></td>
<td width="27"> </td>
<td width="223">Enquiry Date</td>
<td width="160"><input type="text" name="enquiry_date" id="enquiry_date" class="datepicker" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Conference Date In</td>
<td><input type="text" name="conference_date_in" id="conference_date_in" class="datepicker" /></td>
<td> </td>
<td>Conference Date Out</td>
<td><input type="text" name="conference_date_out" id="conference_date_out" class="datepicker" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Number of Delegates</td>
<td><input type="text" name="no_of_delegates" id="no_of_delegates" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Accommodation:</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><p>Check in Date</p></td>
<td><input type="text" name="check_in_date" id="check_in_date" class="datepicker" /></td>
<td><p> </p></td>
<td><p>Check out Date</p></td>
<td><p>
<input type="text" name="check_out_date" id="check_out_date" class="datepicker" />
</p></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>Total Days Accommodation</td>
<td><input type="text" name="total_days_acc" id="total_days_acc" /></td>
</tr>
<tr>
<td>Number of Rooms:</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Single</td>
<td><input type="text" name="no_of_rooms_single" id="no_of_rooms_single" /></td>
<td> </td>
<td>Double / Twin</td>
<td><input type="text" name="no_of_rooms_double" id="no_of_rooms_double" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Contact Person</td>
<td><input type="text" name="contact_person" id="contact_person" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Telephone Number</td>
<td><input type="text" name="tel_no" id="tel_no" /></td>
<td> </td>
<td>Fax Number</td>
<td><input type="text" name="fax_no" id="fax_no" /></td>
</tr>
<tr>
<td>Cell Number</td>
<td><input type="text" name="cell_no" id="cell_no" /></td>
<td> </td>
<td>Email</td>
<td><input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Full Day Conference # R260.00 p/p</td>
<td><input type="text" name="full_day" id="full_day" /></td>
<td> </td>
<td>Total Full Day</td>
<td><input type="text" name="total_full" id="total_full" readonly="readonly" /></td>
</tr>
<tr>
<td>Half Day Conference # R240.00 p/p</td>
<td><input type="text" name="half_day" id="half_day" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Single Rooms # R480.00 p/p</td>
<td><input type="text" name="single_rooms" id="single_rooms" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Double / Twin Rooms # R720.00 p/p</td>
<td><input type="text" name="double_rooms" id="double_rooms" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Data Projector # R400.00 rental p/day</td>
<td><input type="text" name="data_proj" id="data_proj" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Sub Total</td>
<td><input type="text" name="sub_total" id="sub_total" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</form>
</table>
</div>
</body>
</html>
<script type="text/javascript">
$(function() {
$(".datepicker").datepicker({ minDate: -0, maxDate: "+100M +10D",dateFormat: 'dd-mm-yy'})
({
changeMonth: true,
changeYear: true,
});
});
var enquiry_date = $.datepicker.formatDate('dd-mm-yy', new Date());
document.getElementById('enquiry_date').value = enquiry_date;
var calcDate = function() {
var start = $('#conference_date_in').datepicker('getDate');
var end = $('#conference_date_out').datepicker('getDate');
var days = (end - start) / 1000 / 60 / 60 / 24;
if(days==0) days=1
if( days >= 0 ) {
document.getElementById('total_days').value = days;
}
}
$('#conference_date_out').change(calcDate);
$('#conference_date_in').change(calcDate);
var calcDateAcc = function() {
var startacc = $('#check_in_date').datepicker('getDate');
var endacc = $('#check_out_date').datepicker('getDate');
var daysacc = (endacc - startacc) / 1000 / 60 / 60 / 24;
if(daysacc==0) daysacc=1
if( daysacc >= 0 ) {
document.getElementById('total_days_acc').value = daysacc;
}
}
$('#check_in_date').change(calcDateAcc);
$('#check_out_date').change(calcDateAcc);
function calculateFull()
{
var fulldays = document.getElementById("full_day").value;
var fullprice = 260;
var result = fulldays * fullprice;
document.getElementById("total_full").innerHTML = result;
}
$('#full_day').change(calculateFull);
</script>
total_full is input box, so you should set value instead of innetHTML
document.getElementById("total_full").innerHTML = result;
document.getElementById("total_full").value = result;
document.getElementByid("full_day").value returns a string. You have to convert this string into a number before multiplying it with fullprice
var fulldays = parseInt(document.getElementById("full_day").value, 10);
And change document.getElementById("total_full").innerHTML = result;
to document.getElementById("total_full").value = result;

Computing the sum of a variable number of html table columns using Javascript

I have a table with a lot of columns, and each column consists of a text field with a numeric value that can be changed. The last column of the table must hold the sum of all columns. Now here's my question: how can I make the sum change in realtime using javascript? For example if a column value is modified, the sum must be changed in order to fit the new values.
Below is my table:
<table id="sum_table">
<tr>
<td>Column 1</td>
<td><input type="text" name="val1" value="" /></td>
</tr>
<tr>
<td>Column 2</td>
<td><input type="text" name="val2" value="" /></td>
</tr>
<tr>
<td>Column 3</td>
<td><input type="text" name="val3" value="" /></td>
</tr>
[...]
<tr>
<td>Total</td>
<td><input type="text" name="total" value="" /></td>
</tr>
</table>
Thank you.
<html>
<head>
<script type="text/javascript">
function calc()
{
var sum = parseFloat(document.getElementById('val1').value + document.getElementById('val2').value + document.getElementById('val3').value + [...]);
document.getElementById('total').value = sum;
}
</script>
</head>
<body>
<table id="sum_table">
<tr>
<td>Column 1</td>
<td><input type="text" id="val1" value="" OnTextChanged="javascript:calc()"/></td>
</tr>
<tr>
<td>Column 2</td>
<td><input type="text" id="val2" value="" OnTextChanged="javascript:calc()/></td>
</tr>
<tr>
<td>Column 3</td>
<td><input type="text" id="val3" value="" OnTextChanged="javascript:calc()/></td>
</tr>
[...]
<tr>
<td>Total</td>
<td><input type="text" id="total" value="" /></td>
</tr>
</table>
</body>
</html>
That should to the trick.
Edit:
After discussion in the comments, this is the final version: http://jsfiddle.net/gXdwb/3/
$('#sum_table tr:not(.totalColumn) input:text').bind('keyup change', function() {
var $table = $(this).closest('table');
var total = 0;
var thisNumber = $(this).attr('class').match(/(\d+)/)[1];
$table.find('tr:not(.totalColumn) .sum'+thisNumber).each(function() {
total += +$(this).val();
});
$table.find('.totalColumn td:nth-child('+thisNumber+') input').val(total);
});
<table id="sum_table" width="600" border="0">
<tr>
<td>Sum 1</td>
<td>Sum 2</td>
<td>Sum 3</td>
</tr>
<tr>
<td><input type="text" name="textfield" id="textfield" class="sum1" /></td>
<td><input type="text" name="textfield2" id="textfield2" class="sum2" /></td>
<td><input type="text" name="textfield3" id="textfield3" class="sum3"/></td>
</tr>
<tr>
<td><input type="text" name="textfield4" id="textfield4" class="sum1" /></td>
<td><input type="text" name="textfield5" id="textfield5" class="sum2" /></td>
<td><input type="text" name="textfield6" id="textfield6" class="sum3" /></td>
</tr>
<tr>
<td><input type="text" name="textfield7" id="textfield7" class="sum1" /></td>
<td><input type="text" name="textfield8" id="textfield8" class="sum2"/></td>
<td><input type="text" name="textfield9" id="textfield9" class="sum3"/></td>
</tr>
<tr>
<td>Total</td>
<td>Total</td>
<td>Total</td>
</tr>
<tr class="totalColumn">
<td><input type="text" name="textfield10" id="textfield10" /></td>
<td><input type="text" name="textfield11" id="textfield11" /></td>
<td><input type="text" name="textfield12" id="textfield12" /></td>
</tr>
<tr>
<td>bla</td>
<td>bla</td>
<td>bla</td>
</tr>
</table>
As usual, I'm not sure if this is optimal, but it seems to work:
See: http://jsfiddle.net/gXdwb/
$('#sum_table tr:not(:last-child)').bind('keyup change', function() {
var $table = $(this).closest('table');
var total = 0;
$table.find('tr:not(:last-child) input:text').each(function() {
total += +$(this).val();
});
$table.find('input[name="total"]').val(total);
});

Categories