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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:47:00+00:00 2026-05-27T04:47:00+00:00

I have an problem with the code below that’s related to closures, that I

  • 0

I have an problem with the code below that’s related to closures, that I need some help with.

As you can see I am creating several images in the for-loop that i’m assigning different id’s (ie. the numbers from the array). So far, so good. When I click on the different images I want the function showId to be called with the images id’s as argument, but the problem is that the number that’s used as an argument to the function always becomes nr 8 (the last number in the array). How can I solve this?

Thanks in advance.

var imageArea = document.getElementById('imageArea');
var nrArray = [1,2,3,4,5,6,7,8];

for (var i = 0; i < nrArray.length; i++){
    var image = document.createElement('img');
    image.src = "images/theimage.png";
    image.id = nrArray[i];
    imgArea.appendChild(image);
    image.addEventListener ('click',function() {showId(image.id);},false);
}
  • 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-27T04:47:01+00:00Added an answer on May 27, 2026 at 4:47 am

    There are a bazillion answers to this exact same question here on stackoverflow, i’ll hunt some down in a minute, but here are the key points:

    • Javascript has lexical scope (not dynamic scoping)
    • The smallest Javascript scope in javascript is a function
    • Block do not have thier own scope
    • Javascript uses variable hoisting, which means all variables in a scope are found before the scope executes and moved to the beginning of the scope. Which means that var image = ... isn’t where you think it is. There is only one image variable, which (due to lexical scoping) is the variable you are accessing in your closure, which at the conclusion of the loop will obviously point at the last iterated item.
    • Functions are first-class objects, which means they are treated like variables and can be assigned and passed around as such
    • you can create a stable scope for a variable to live in by creating a self-executing anonymous function

    So for example:

    (function(localImage) {
        image.addEventListener ('click',function() {showId(localImage.id);},false);
    })(image);
    

    Also, as others have pointed out, event listener closures are executed in the context that they were bound. So conversely, without having worry about using a close to fix a scope, you could do:

    image.addEventListener ('click',function() {showId(this.id);},false);
    

    Edit

    Some links to similar questions and some different perspectives on the answer:

    • Accessing loop iteration in a sub-function?
    • Closures in a for loop and lexical environment
    • Strange things in JavaScript “for”
    • Event handlers inside a Javascript loop – need a closure?

    I could go on but a bazillion is a big number…

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

Sidebar

Related Questions

I have some problem with my cfml website. I have used the below code
I have a very specific problem. I have a user control (code below) That
As you can see in the code below, I have an Abstract Base Class
I have problem with session and query execution,please see code below. class A implements
I have problem with session and query execution, please see code below. class A
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have some problem with my code public IQueryable<PageItems> GetPageById(Guid Id) { var xml
I have a problem understanding what's causes the compilation error in the code below:
I have a problem with the code below. Compiler says incompatible types, java.lang.object[][] required
I have a problem with my code posted below, set and get methods. I

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.