0

I have the code below:

$("input.isDebt").click(function() {
    var totalAmount_test = $("#CC_TotalAmount_form").val();
    var total = 0;
    $("input.isDebt:checked").each(function() {
        total = parseFloat($(this).val());
        });

        totalAmount_test = parseFloat(totalAmount_test)+total;
    });
$("input.isDebt").blur(function() {
    sum = totalAmount_test + parseFloat($("#CC_TotalAmount_form").val());
    $("#CC_TotalAmount_form").val(sum.toFixed(2));


});


});

I need totalAmmount_test to be passed from the $("input.isDebt:checked").each(function() { ... }) into the function $("input.isDebt").blur(function() { ... }).

Any ideas on how I can do this?

1
  • try declaring totalAmmount_test outside the click function.. Commented Nov 16, 2011 at 2:12

3 Answers 3

1

The best way would be to use .data() (assuming your using jQuery). More info on that here

$("input.isDebt").click(function() {
    var totalAmount_test = $("#CC_TotalAmount_form").val();
    var total = 0;
    $("input.isDebt:checked").each(function() {
        total = parseFloat($(this).val());
        });

        totalAmount_test = parseFloat(totalAmount_test)+total;
        $(this).data('totalAmount_test', totalAmount_test);
    });
$("input.isDebt").blur(function() {
    totalAmount_test = $(this).data('totalAmount_test');
    sum = totalAmount_test + parseFloat($("#CC_TotalAmount_form").val());
    $("#CC_TotalAmount_form").val(sum.toFixed(2));
});
Sign up to request clarification or add additional context in comments.

Comments

0

Sure..

var totalAmount_test = 0;
$("input.isDebt").click(function() {
    totalAmount_test = $("#CC_TotalAmount_form").val();
    var total = 0;
    $("input.isDebt:checked").each(function() {
        total = parseFloat($(this).val());
        });

        totalAmount_test = parseFloat(totalAmount_test)+total;
    });
$("input.isDebt").blur(function() {
    sum = totalAmount_test + parseFloat($("#CC_TotalAmount_form").val());
    $("#CC_TotalAmount_form").val(sum.toFixed(2));
});
});

Would be the easiest way.

1 Comment

I am using the code with radio buttons, it is passing the values ok but is incrementing the total. for example - I have a total of 50 and I have two radio buttons one with a value of 10 and the other with a value of 20 when I click the radio button with a value of 10 the total will become 60, this is working ok. but if I change my mind and choose the radio button with the value of 20 the total then becomes 80 instead of being 70. I hope this makes sense to somebody
0

create an element with a data attribute, make sure to prefix custom attributes with 'data-' to be HTML5 compliant

$("input.isDebt").click(function() {
    var totalAmount_test = $("#CC_TotalAmount_form").val();
    var total = 0;
    $("input.isDebt:checked").each(function() {
        total = parseFloat($(this).val());
        });

        totalAmount_test = parseFloat(totalAmount_test)+total;

        // creates a hidden element w/the amount
        $("<div id='total-amount' style='display:none'></div>").attr("data-total-amount", totalAmount_test); 
    });

$("input.isDebt").blur(function() {
    // retrieve the value
    var totalAmount = $("#total-amount").attr("data-total-amount"); 

    // remove the div if you don't need it anymore
    $("#total-amount").remove(); 

    sum = totalAmount_test + parseFloat($("#CC_TotalAmount_form").val());
    $("#CC_TotalAmount_form").val(sum.toFixed(2));
});


});

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.