0

This my code:

$('#paying_by').click(function()
{       
       if ($(this).val() == "GBPTRF")
       {
          $('.currency').html('&#163 ');
          $('#c_card_rate').val("0.00");
          $('#c_card_amount').val("0.00");
          $('.total_inc_charges').val(); /*???? refresh? this is the question*/
          $('.c_card_field').hide();            
       }                

       if ($(this).val() == "GBPCCARD")
       {
          $('.currency').html('&#163 ');
          $('.c_card_field').show();    
       }    

});

By default the c_card_field is hidden. If the user clicks on GBPCCARD then 2 fields appear within the c_card_field. One of them is the c_card_rate which is a %. Then the total amount increses with amount_due*c_card_rate/100. If the user selects GBPTRF again then, the c_card_field disappears. Of course I clear the the c_card_rate field but the total amount which has been increased by the c_card_amount(amount_due*c_card_rate/100) remains increased until I amend the amoun_due field.

Question: How can I refresh the total_inc_charges when the user clicks GBPTRF again?

Thank you.

2
  • Why don't you recalculate the total_inc_charges? Commented Dec 10, 2011 at 9:58
  • I used jquery.calculation for the calculation. It looks like this $(function() { $('input[name^=sum]').keyup(function() { var sum1 = parseFloat($('input[name=sum1]').val()) || 0; var sum2 = parseFloat($('input[name=sum2]').val()) || 0; var sum3 = parseFloat($('input[name=sum3]').val()) || 0; $('#total_inc_charges').val((sum1*sum3/100)+sum1); }); }); and I'm not really sure how to do it out of this function... Commented Dec 10, 2011 at 10:03

1 Answer 1

1

Based on your last comment. I would refactor your code like this:

var calc_total_inc_charges = function () {
    var sum1 = parseFloat($('input[name=sum1]').val()) || 0;
    var sum2 = parseFloat($('input[name=sum2]').val()) || 0;
    var sum3 = parseFloat($('input[name=sum3]').val()) || 0;
    return (sum1*sum3/100)+sum1;
};

$(function() {
    $('input[name^=sum]').keyup(function() {
        $('#total_inc_charges').val(calc_total_inc_charges());
    });
});

Now you can call calc_total_inc_charges() whenever you like to recalculate charges.

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

2 Comments

Thank you for your help. Then I realized that without the credit card charges the actual total including charges is none of other than amount_due. so I did this: $('#total_inc_charges').val(($('#amount_due').val()));
I don't even know why I used jquery.calculation... Lot easier to work with .val(). Learning... :)

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.