0

I need to be using the firmType captured on event 1 on event 2. How to accomplish that?

code

event 1

  $("body").on("change","#so_customer", function(v){
        customerFirm = $(this).find(":selected").data("employer");
        $("#customer_employer").val(customerFirm);
        $("#historyLink").show();
        $("#historyLink").attr("href",GSVTOOL.AJAX.BASE +  

        $container = $("#hp_deposit_id");
        $container.select2('val', '');

        firmType = $(this).find('option:selected').data("firmtype");

    });

event 2

   $("body").on("change", "#installment_id", function(){
        $container = $("#hp_deposit_id");
        $container.select2('val', ''); 
        instId = $(this).val();
        alert(firmType);
        url = 'erp/sales-orders/ajax-initial-deposit';
        data = 'firmType='+firmType+'&instId='+instId;
        loadDepositPlan($container, url, data);
    });

Please help me on this.

3
  • Are your 2 events handlers on the same page ? If yes, can't you just use a common variable that you would modify into event1 then retrieve in event2 ? Commented May 12, 2015 at 13:33
  • Declare the variable as global so you will be able to use them in both event handlers. Commented May 12, 2015 at 13:36
  • You also need to make sure that the variable is set when event2 is triggered. Commented May 12, 2015 at 13:42

2 Answers 2

2

The quickest solution would be to define the firmType variable outside the scope of your two event handler functions, like so:

var firmType;

$("body").on("change","#so_customer", function(v){
    // [Code to find firmType]

    firmType = $(this).find('option:selected').data("firmtype");

});

$("body").on("change", "#installment_id", function(){
    alert(firmType) // Will display value as set in handler 1
});

as Moolie states, the var keyword locks the scope of the variable to the current scope and any scopes within it. Thus by defining it outside your two handlers, and omitting var from your references to firmType inside your first handler, the variable's value is updated "globally" in the first handler and therefore available in the second.

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

Comments

1

You can use a global variable, but it appears you are already doing that.

You can define a local variable by using the var keyword in a function. If you omit this keyword or define the variable outside a function, then the variable will have global scope (meaning it will be known everywhere in your javascript code). Any global variables are actually properties of the window object btw.

For example :

var myGlobalVar = "This is a global variable";  // var in global scope, so a global var
function myFunction() {
    var myLocalVar = "This is a local variable";  // var in function scope, so a local var
    myGlobalVar2 = "Another global variable";  // No var used, so a global var
}

alert(myGlobalVar);  // Will alert 'This is a global variable'
alert(myLocalVar);  // Will alert 'undefined', as myLocalVar is not defined in global scope
alert(myGlobalVar2); // Will alert 'Another global variable'

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.