Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6787907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:25:19+00:00 2026-05-26T17:25:19+00:00

I have this function function update_prices(product_selector){ //kind of a hack to account for the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:25:20+00:00Added an answer on May 26, 2026 at 5:25 pm

    To keep those variables persistent after the function declaration, you have these choices:

    1. They have to be declared at a higher level scope that persists for the duration you need them.
    2. They need to be global variables that last for the life of the web page.
    3. They need to be assigned as properties of some object that persists for the desired duration. This could be done by returning an object with these values from the function or passing an object into the function that properties can be set on or by having a global object that put the properties on.

    The simplest solution (thought not always the best) is to make them global variables by declaring them at a higher scope and removing the var in front of them in the function so you are just operating on the global variables instead of using local variables:

    // declare global variables in global scope
    var standard_selector;
    var business_selector;
    var profession_selector;
    
    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){
                standard_selector = product_selector.find('small.rt:eq('+ i +')');
                var standard_price = standard_selector.attr('data');
            }
            if(i == 1){
                business_selector = product_selector.find('small.rt:eq('+ i +')');
                var business_price = business_selector.attr('data');
            }
            if(i == 2){
                professional_selector = product_selector.find('small.rt:eq('+ i +')');
                var professional_price = professional_selector.attr('data');
            }
        }
    }
    

    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:

    function update_prices(product_selector){
        //kind of a hack to account for the sometimes having a professional price and sometimes not
        var sel = {};
        var price_count = product_selector.find('small.rt').length;
        for (i=0;i<=price_count;i++)
        {
            if(i == 0){
                sel.standard_selector = product_selector.find('small.rt:eq('+ i +')');
                var standard_price = standard_selector.attr('data');
            }
            if(i == 1){
                sel.business_selector = product_selector.find('small.rt:eq('+ i +')');
                var business_price = business_selector.attr('data');
            }
            if(i == 2){
                sel.professional_selector = product_selector.find('small.rt:eq('+ i +')');
                var professional_price = professional_selector.attr('data');
            }
        }
        return(sel);
    }
    
    var selectors = update_prices(xxx);
    // access selectors.standard_selector, selectors.business_selector, selectors.profession_selector here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function in my head: <head> window.onload = function(){ var x =
I have an ajax request that looks like this, $(#frmProducts).submit(function(){ var dataSet = $(#frmProducts).serialize();
I have an if statement that needs to look like this: UPDATE $(input#textbox).keypress(function(e){ key==e.which;
I have this function in my Javascript Code that updates html fields with their
I have this function from a plugin (from a previous post) // This method
I have this function... private string dateConvert(string datDate) { System.Globalization.CultureInfo cultEnGb = new System.Globalization.CultureInfo(en-GB);
I have this function: RegisterGlobalHotKey(Keys.F6, MOD_SHIFT | MOD_CONTROL); which call an API to register
I have this function to read in all ints from the file. The problem
I have this function: public bool IsValidProduct(int productTypeId) { bool isValid = false; if
I have this function signature I have to match typedef int (*lua_CFunction) (lua_State *L);//target

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.