I have the following function:
function initPaymentBlock(bIdx) {
var paymentType = 'default';
if( $('#gc-apply-btn-'+bIdx).length ) {
$('#gc-apply-btn-'+bIdx).click( function() {
poApplyGiftCard(bIdx);
console.log("about to change payment type");
paymentType = 'credit card';
console.log("paymentType inside function: " + paymentType);
});
}
console.log("payment type: " + paymentType);
poGetPaymentOption(paymentType, 0, bIdx);
}
The paymentType variable is set to 'default' inside the initial statement. Inside the internal click function, it's changed to 'credit card'. When I run the console.log statement inside the internal function, it returns credit card. The console.log outside the internal function (right before the poGetPaymentOption call) returns 'default'.
I know the issue is the scope of the variable, but can't figure out how or where I can set it so that the poGetPaymentOption function call gets the correct value ('default' if the button isn't clicked before it's run; 'credit card' if the button is clicked.) Can someone help me out?
poGetPaymentOptioninside the click event? It really depends on what you are trying to do and WHEN you are trying to do it.paymentTypewill always be its initial value whenpoGetPaymentOptiongets run because it can't change until someone clicks something, and from what you just saidpoGetPaymentOptiongets run wheninitPaymentBlockgets run.