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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:40:33+00:00 2026-05-26T22:40:33+00:00

for (id = 50; id < 100; id++) { if($(‘#’+id).attr(‘class’) == ‘myField’) { $(‘#’+id).bind(‘click’,

  • 0
for (id = 50; id < 100; id++)
{
    if($('#'+id).attr('class') == 'myField')  
    {
            $('#'+id).bind('click', function() { install(id); } );
    }
}

No idea why id can’t reach ‘install’ in function(). I am trying to bind every button (id from 50 to 100) with a click event to trigger the install(id) function. But it seems the variable id cannot reach install function. While I hard code it:

for (id = 50; id < 100; id++)
{
      if($('#'+id).attr('class') == 'myField')  
      {
            $('#'+id).bind('click', function() { install( 56 ); });
      }
}

it works! Please tell me why.

  • 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-26T22:40:34+00:00Added an answer on May 26, 2026 at 10:40 pm

    What you made is one of the most common mistakes when using Javascript closures.

    By the way the very fact that this mistake is so common is IMO a proof that it’s indeed a “bug” in the language itself.

    Javascript supports read-write closures so when you capture a variable in a closure it’s not the current value of the variable that is captured, but the variable itself.
    This means that for example in

    var arr = [];
    for (var i=0; i<10; i++)
        arr.push(function(){alert(i);});
    

    each of the 10 functions in the array will contain a closure, but all of them will be referencing the same i variable used in the loop, not the value that this variable was having at the time the closure was created. So if you call any of them the output will be the same (for example 10 if you call them right after the loop).

    Luckily enough the workaround is simple:

    var arr = [];
    for (var i=0; i<10; i++)
        arr.push((function(i) {
                    return (function(){alert(i);});
                  })(i));
    

    using this “wrapping” you are calling an anonymous function and inside that function the variable i is a different one from the loop and is actually a different variable for each invocation. Inside that function i is just a parameter and the closure returned is bound to that parameter.

    In your case the solution is therefore:

    for (id = 50; id < 100; id++)
    {
        if($('#'+id).attr('class') == 'myField')  
        {
            $('#'+id).bind('click',
               (function(id){
                     return (function() { install(id); });
                })(id));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

An interface is a 100% abstract class, so we can use an interface for
I work on a 100% Javascript site that uses a CSS class on the
I have about 100 elements in like and am trying to create an animation
A site has 100's of pages, following a certain sitemap. A user can navigate
Suppose i have 100 instances of a class. I want to make arrays of
100['toString'] //does not fail 100.toString //fails why? 100.toString is not same as 100.toString() .
100%I'm modifying an older website for a client that uses Ext-JS 1.1 and I'm
Is a GUID unique 100% of the time? Will it stay unique over multiple
While there are 100 ways to solve the conversion problem, I am focusing on
I need to manipulate 100,000 - 200,000 records. I am thinking of using LINQ

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.