I have this function
function update_prices(product_selector){
//kind of a hack to account for the sometimes having a professional price and sometimes not
var price_count = product_selector.find('small.rt').length;
for (i=0;i<=price_count;i++)
{
if(i == 0){
var standard_selector = product_selector.find('small.rt:eq('+ i +')');
var standard_price = standard_selector.attr('data');
}
if(i == 1){
var business_selector = product_selector.find('small.rt:eq('+ i +')');
var business_price = business_selector.attr('data');
}
if(i == 2){
var professional_selector = product_selector.find('small.rt:eq('+ i +')');
var professional_price = professional_selector.attr('data');
}
}
}
and i have this chunk of code that calls it
....
....
product_selector.find(".active_selector").removeClass('active_selector');
update_prices(product_selector);
....
....
standard_selector.text("something");
business_selector.text("something else");
professional_selector.text("another thing");
My question is how do i keep the scope for the three variables standard_selector business_selector and professional_selector that get created in the update_prices function
To keep those variables persistent after the function declaration, you have these choices:
The simplest solution (thought not always the best) is to make them global variables by declaring them at a higher scope and removing the
varin front of them in the function so you are just operating on the global variables instead of using local variables:Or, if you just want to return them from this function so you can use them one level up in scope, then you could return them in an object: