Javascript variable passing between function

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?

Answers


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));
});

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.


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));
});


});

Need Your Help

Create a class in C# such that it can't be instantiated and satisfy following conditions :

c# oop

Create public class in C# such that following conditions are satisfied :

PHP Remove all Files From a Directory - Exclude File Extension

php regex preg-match glob

I am trying for the life of me to find the best way to delete all files in a single directory excluding a single file extension, ie anything that is not .zip

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.