I have basic order form, i would like to sum values form all inputs . But when I add items program adds the values to a string insted summarizing them (totalUnits).
Working jsfiddle: http://jsfiddle.net/nitadesign/97tnrepg/48/
and the part of a code to bring your attention to a right place
function CalculateTotal(){
var total = 0;
for(i = 0; i< orders.length; i++){
total = total + orders[i].packTotal;
}
console.log(total);
console.log(totalUnits);
if(total > 0){
var counter = 0;
$("input[type=text]").each(function(){
if($(this).val() != "" && $(this).val() != 0) counter++;
});
var totalUnits = 0;
$("input[type=text]").each(function(){
packUnit = $(this).val();
totalUnits = totalUnits + packUnit;
});
$("#order_total").html('Ordered Products:' + counter + '<br>' + 'Total Items:' + totalUnits + '<br>' + 'Order Subtotal: ' + total);
$("#order_total").show();
$('.submitorder').show();
}
if(total == 0){
$("#order_total").html('Your shopping basket is empty');
$("#order_total").show();
$('.submitorder').hide();
}
}
Thanks a lot for you help.
You have to use parseInt() to convert strings into integers
Use parseInt
when you get inputs values :
var packPrice = parseInt($('#pack' + curId + '-price').val());
var packUnit = parseInt($(this).val());
And in your calculateTotal()
function also :
$("input[type=text]").each(function(){
packUnit = parseInt($(this).val());
totalUnits = totalUnits + packUnit;
});