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 3961508
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:59:01+00:00 2026-05-20T02:59:01+00:00

Sorry about the title – I couldn’t figure out a way to phrase it.

  • 0

Sorry about the title – I couldn’t figure out a way to phrase it.

Here’s the scenario:

I have a function that builds a element:

buildSelect(id,cbFunc,...)

Inside buildSelect it does this:

select.attachEvent('onchange',cbFunc);

I also have an array that goes:

var xs = ['x1','x2','x3'...];

Given all of these, I have some code that does this:

for(var i = 0; i < xs.length; i++)
{
    buildSelect(blah,function(){ CallBack(xs[i],...) },...);
}

The issue is that when onchange gets fired on one of those selects it correctly goes to CallBack() but the first parameter is incorrect. For example if I change the third select I expect CallBack() to be called with xs[2] instead I get some varying things like xs[3] or something else.

If I modify it slightly to this:

for(var i = 0; i < xs.length; i++)
{
    var xm = xs[i];
    buildSelect(blah,function(){ CallBack(xm,...) },...);
}

I’m still getting incorrect values in CallBack(). Something tells me this is scope/closure related but I can’t seem to figure out what.

I simply want the first select to call CallBack for onchange with the first parameter as xs[0], the second select with xs[1] and so on. What could I be doing wrong here?

I should clarify that xs is a global variable.

Thanks

  • 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-20T02:59:01+00:00Added an answer on May 20, 2026 at 2:59 am

    You need to capture that xm value by closing around it in its own scope.

    To do this requires a separate function call:

    buildCallback( curr_xm ) {
    
          // this function will refer to the `xm` member passed in
        return function(){ CallBack(curr_xm,...) },...);
    }
    
    for(var i = 0; i < xs.length; i++)
    {
        var xm = xs[ i ];
        buildSelect(blah,buildCallback( xm ),...);
    }
    

    Now the xm that the callback refers to is the one that you passed to buildCallback.

    If you have other uses for i that need to be retained, you could send that instead:

    buildCallback( curr_i ) {
    
    
          // this function will refer to the `i` value passed in
        return function(){ CallBack( xs[ curr_i ],...) },...);
    }
    
    for(var i = 0; i < xs.length; i++)
    {
        buildSelect(blah,buildCallback( i ),...);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, sorry about the title -- I couldn't figure out one that
[Sorry about the clumsy title - I couldn't figure out the best way to
Sorry about the title, I couldn't think of a better way to describe the
Sorry about the terrible title. I have a signed assembly 'Signed.dll' that is a
Sorry about the title, I couldn't come up with a clean way to ask
Sorry about the title I didn't quite know how to phrase it. I have
(Sorry about the title, couldn't think of how to explain it) So I have
Sorry about the vague question title, but I have these typedefs here: typedef std::list<AnimationKeyframe>
Sorry about the title, don't know how to phrase this. I have my own
Sorry about the question title, but I couldn't find a more appropriate way to

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.