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

  • Home
  • SEARCH
  • 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 7528547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:24:38+00:00 2026-05-30T04:24:38+00:00

I am prob. being pretty dense here but I can’t figure out exactly what

  • 0

I am prob. being pretty dense here but I can’t figure out exactly what is going on in the code below.

What I am trying to do is attach two, separate, handlers to the change event of a field. Each handler is set up by looping over an array and using the items in the array to effect the output of the handler when it runs – hopefully will become clear when you look at the example code.

Code follows:

    $(document).ready( function () {
        //
        // Create some test input fields on the page...
        //
        $('<br />').insertAfter($('body > *:last'));
        $('<input type="text" name="t0" id="t0" value="" />').insertAfter($('body > *:last'));
        $('<input type="text" name="t1" id="t1" value="" />').insertAfter($('body > *:last'));

        //
        // The problematic part  - for me at least...
        //
        var arr = new Array(1, 2);
        for (var a in arr) {
            // Using Chrome console here for logging
            console.log("## " + a);
            $('#t0').change(function () {
                console.log(">> " + a)
            });
        }
    });

So what I would expect to happen when I add a value to the first field is, from within the console (running these examples within Chrome):

## 0
## 1
>> 1
>> 2

What I get is:

## 0
## 1
>> 1
>> 1

I would have expected the function passed to the handler would form a closure over the value of a and I would end up with two functions being bound to the handler, one in which a had the value 1 and one in which a had the value 2.

Ideas?

Cheers – kris

  • 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-30T04:24:40+00:00Added an answer on May 30, 2026 at 4:24 am

    There are two big mistakes here:

    First of all, the for (a in x) doesn’t work like you expect it to: it iterates over object properties, not over array elements.

    The other mistake is that a changes by the time the function gets called. A good way to achieve the desired functionality is like this:

    for(var a=0; a<arr.length; a++) {
        (function(a) {
            // now you can use "a"
        })(arr[a]);
    }
    

    To see what happens with the for loop if you don’t create a closure, see this:

    var arr = [1,2,3];
    var functions = [];
    
    for(var a=0; a<arr.length; a++) {
        functions.push(function() {
            console.log(a);
        })
    }
    
    // now execute all the functions
    for(var i=0; i<functions.length; i++) {
        functions[i]();
    }
    

    Now all the functions will log 3, which is the index of the last element in the array + 1 (arr[0] == 1, arr[1] == 2, arr[2] == 3). What happens is that the for loop creates those functions at each iteration, but they get executed after the loop finishes, when a == arr.length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can put value into array and here is my code and prob. Search.h
this is prob pretty easy but if someone could explain the easiest way to
I am not going to paste the code here and hope the statement of
I expierencing something strange. Prob it's easy, but my css knowhow stops here. In
Prob something im overlooking but this problem has annoyed me greatly. im trying to
It must be Monday, the heat or me being stupid (prob the latter), but
This is prob. very simple but I can't get it. I have a href
This question is prob a duplicate (but I can't find it) so I apologize
Can anyone tell me what's the prob with the following code? outputStream.writeBytes(twoHyphens + boundary
Here is my prob in Brief. I know how to upload a file 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.