0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.