views:

347

answers:

1

Hello,

I’m adding a total amount value to a DIV through a jQuery Ajax call everytime I add a new item to a shopping cart. I need this value to be a part of a difference calculation (payment-totalAmount), but I’m having troubles getting the totalAmount value.

I set the total amount in the function called SetTotalAmount and then I try to get the value from the DIV tag in the submitPayment actionevent:

<script type="text/javascript">
$(document).ready(function(){

    $("#submitPayment").click(function(){
     var paymentAmount = $("#paymentAmount").val();
     var totalAmount = $("#totalTillAmount").val();
     var difference = (paymentAmount-totalAmount);

     $("#paymentTillAmount").html("betalt: "+paymentAmount);
     //$("#totalTillAmount").html("total: "+totalAmount);

     $("#difference").html("Tilbage: "+difference);

     $("#paymentInfo").show('slow');

    });

});


function SetTotalAmount()
{
    $.post("Controller/TillController.php?action=3", 
      function(data)
      {
       $("#totalAmount").html(data);
       $("#totalTillAmount").html(data);
      }
    );
}
</script>
+1  A: 

You may need to parse strings before performing calculations:

var paymentAmount = parseFloat($("#paymentAmount").text());
var totalAmount = parseFloat($("#totalTillAmount").text());

Also as #totalTillAmount is a div you may need to use the text function instead of val (which is used for input elements) to read it's contents.

Darin Dimitrov