How to add values from all input with name name="TotalInline[]"?
The following does not seams to work:
var total = 0;
$.each('input[name="TotalInline[]"];,function() {
total += this;
});
This should work :
var total = 0;
$('input[name="TotalInline"]').each(function() {
// assuming you have ints in your inputs, use parseFloat if those are floats
total += parseInt(this.value, 10);
});
var total = 0;
$.each($('input[name="TotalInline[]"]'), function() {
total += parseInt(this.value, 10);
});
You have some serious syntax errors, try this:
var total = 0;
$('input[name="TotalInline[]"]').each(function () {
total += parseInt(this.value, 10);
});
Try like this...
var total = 0;
$('input[name="TotalInline[]"]').each(function() {
total += parseInt($(this).val(),10);
});
var total = 0;
$('input[name="TotalInline[]"]').each(function() {
total += +this.value.replace(/[^\d.]/g, '');
});
Uses a quick regex to filter out only the numbers (and decimal point).
Uses a + prefix to convert to a number.
Related
i have a js code that calculate the total amount when being click by checkbox. The user can either choose to select all or select only certain amount that they want to calculate. The function if the user 'select all'is work fine but when they click only certain amount that they want to calculate, if the amount is have ','in it, it will only take the number before the ','for example if the amount is 20,230, it will total the amount as 20.00. it work real fine if the number is only 20230 without the ','. help me please.
function checkedAll () {
var row_counter =0;
$("input[id ^= 'check_'] ").each(function() {
row_counter++;
});//en
var total=0;
for(var i=1; i<=row_counter; i++) {
//alert(i);
if (document.getElementById("checkall").checked==true){
document.getElementById('check_'+i).checked = true;
var amaun = document.getElementById('amaun_'+i).value;
//alert(amaun);
total = parseFloat(total)+parseFloat(amaun);
}
else{
document.getElementById('check_'+i).checked = false;
var amaun = 0;
total = parseFloat(total)+parseFloat(amaun);
}
}
document.getElementById("jum_baucar").value=total.toFixed(2);
}
function calcTotal(){
//alert("here");
var row_counter =0;
$("input[id ^= 'check_'] ").each(function() {
row_counter++;
});//en
var total=0;
for(var i=1; i<=row_counter; i++) {
//alert(i);
if (document.getElementById('check_'+i).checked == true){
var amaun = document.getElementById('amaun_'+i).value;
//alert(amaun);
total = parseFloat(total)+parseFloat(amaun);
}
else{
var amaun = 0;
total = parseFloat(total)+parseFloat(amaun);
}
}
document.getElementById("jum_baucar").value=total.toFixed(2);
}
Those are my js code.
what should I add into it?
Having a , in a number that is applied with parseFloat() treats it as a decimal. For example parseFloat('10,10') => 10. One simple solution is simply removing the , from the string:
amaun = amaun.replace(',',''); // Replace a comma with nothing
Or for several commas using a regexp to act as a "replaceAll()":
amaun = amaun.replace(RegExp(',','g'),'');
Then you can apply parseFloat(amaun):
total = parseFloat(total)+parseFloat(amaun);
I'm trying to sum values from an input array but the sum it keeps returning NAN
var sum = 0 //i have also tried parse(0,10);
$(qty_name).each(function(){
var aValue = $(this).val(); //i have tried parseInt($(this).val(), 10)
sum += aValue; //i have tried sum = parseInt(sum) + parseInt(aValue)
});
alert(sum);
i keep getting NaN. I'm coming from a php background so i've never had to deal with type casting. Please what am i missing?
That is because your other qty_name do not have perfect integer value. which results to NAN for such values. You need to parse the values to int(or float) for doing any mathematical calucations:
var sum = 0;
$(qty_name).each(function(){
sum += parseInt($(this).val()) || 0;
});
alert(sum);
Do like this:
var sum = 0;
$(qty_name).each(function(){
sum += +($(this).value()); //+ converts to a number
});
alert(sum);
You were doing wrong with var aValue += $(this).val() where you're defining the variable you should not use += operator. You should have done like this:
var sum = 0 //i have also tried parse(0,10);
$(qty_name).each(function(){
var aValue = $(this).val(); //removed + sign before = sign
sum += aValue; //i have tried sum = parseInt(sum) + parseInt(aValue)
});
alert(sum);
As far as i understand from your question, you try to avoid adding NaN, use the jQuery built-in method .isNumeric() for that like (and of course += on undefined doesn't end well):
var sum = 0;
$(qty_name).each(function(){
var aValue = parseInt($(this).val(), 10);
if(!$.isNumeric(aValue)) return;
sum += aValue;
});
alert(sum);
Try using isNaN and parseInt as shown :
var sum = 0;
$(qty_name).each(function(){
sum += (isNaN($(this).val()) ? 0 : parseInt($(this).val(),10));
});
alert(sum)
You need to use parseInt or parseFloat, but here you are using var aValue += $(this).val(); and aValue is not initialised. Directly add value to sum
var sum = 0;
$(qty_name).each(function(){
sum += parseInt($(this).val(), 10)||0;
});
alert(sum);
I want that if a button is clicked, 0.99 will be added to my var amount. I cant get this done and i dont know why. Anyone an idea?
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
var amount = 0.99++;
}
});
console.log(amount);
Use var amount += 0.99; instead of using var amount = 0.99++;
Try,
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
var amount += parseFloat(0.99);
}
});
console.log(amount.toFixed(2));
Try this:
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
var amount += parseFloat(0.99);
}
});
console.log(amount);
parseFloat
Change to
amount += 0.99;
The problem with your code is that your doing a post increment which means you will increment after the assignment. This means you won't see the effects of the increment in the amount variable.
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
amount += 0.99;
}
});
console.log(amount);
var amount = 0;
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
var amount += 0.99;
}
});
console.log(amount);
Try this,
<script type="text/javascript">
var amount = 0;
$(function(){
$('.factuur-post').click(function(){
if($(this).is(':checked')) {
amount = amount + 0.99;
}
console.log(amount);
});
console.log(amount);
});
</script>
var amount = 0.99++;
doesn't work.
What ++ does is increment a var by 1.
In your code 0.99 isn't a variable, therefore it can't increment it. And even if it could, amount would always be equal to 1.99 (0.99 + 1 = 1.99).
So to fix it, you need to increment a variable. Except you're not incrementing by 1, you're incrementing by 0.99, so don't use ++ here.
Just use:
amount += 0.99
That means amount = amount + 0.99
I need to enter a set of numbers in a textbox in this form 12, 13, 54, 12 and calculate the average of the numbers from the textbox.
Is their any form or something with javascript or jquery to calculate this on realtime or without post back.
Thanks
$(document).ready(function(){
$("#yourtextbox").keyup(function(){
var text = $(this).val();
var arr = text.split(',');
var sum = 0;
var count = 0;
$.each(arr,function(index,value){
var currentItem = parseInt(value);
if (!isNaN(currentItem)){
sum+= currentItem;
count++;
}
});
console.log(sum/count);
$("#result").text(sum/count);
});
});
Demo
$('.x').keyup(function(){
var total = 0;
$('.x').each(function()
{
var value = parseInt($(this).val());
if(!isNaN(value))
total += value;
})
alert(total);})
I saved a fiddle http://jsfiddle.net/hcs49/; instead of using class selector, you could also use input[type=text]
I have two form inputs which display the calculated invoice sub-total and total of a form.
The problem I'm having is it's not displaying 2 decimal places.
Subtotal function:
function calcProdSubTotal() {
var prodSubTotal = 0;
$(".row-total-input").each(function(){
var valString = $(this).val() || 0;
prodSubTotal += parseInt(valString);
});
$("#product-subtotal").val(prodSubTotal);
};
Total function
function calcOrderTotal() {
var orderTotal = 0;
var productSubtotal = $("#product-subtotal").val() || 0;
var productTax = $("#product-tax").val() || 0;
var orderTotal = parseInt(productSubtotal) + parseInt(productTax);
var orderTotalNice = "$" + orderTotal;
$("#order-total").val(orderTotalNice);
};
How do I go about displaying two decimal places?
change $("#product-subtotal").val(prodSubTotal);
to $("#product-subtotal").val(addDecimals(prodSubTotal));
and change $("#product-subtotal").val(prodSubTotal);
to $("#product-subtotal").val(addDecimals(prodSubTotal));
function addDecimals(a){
a += "";
var i=a.indexOf('.');
if(i<0){
return a + ".00";
}
var j = a.substring(i);
console.log(j);
if(j.length<3){
for(var k=j.length;k<3;k++)
a+='0';
return a;
}
if(j.length>3){
return a.substring(0, i)+j.substring(0,3);
}
}
You may want to look here:
http://www.mredkj.com/javascript/nfbasic2.html
Basically you can use toFixed(2), but then you get some rounding.
Or, if rounding is bad you can do parseInt(productTax * 100) / 100.
If you are working with real numbers it would be better to use parseFloat instead of parseInt. To format the number you could use the toFixed function:
$("#product-subtotal").val(prodSubTotal.toFixed(2));