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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:33:42+00:00 2026-06-03T13:33:42+00:00

I know, great title right ;-) Anyway, lets say I have the following code

  • 0

I know, great title right 😉

Anyway, lets say I have the following code (which I do):

function GetArray(i) {
    if (i == 0) {
        return [1, 2, 3];
    }
    return [];
}

for (var i = 0; i < 3; i++) {
    var array = GetArray(i);

    var onClick = function() {
        alert(array.length);
    }

    var html = "<a>click me</a><br/>";
    var element = $(html);
    $("div").append(element);

    element.click(onClick);
}​

See working code here

Click the 3 links, and notice the alert with the value of 0 for each.

What I want is for the first link to alert 3 when clicked.

Now, before you all start shouting why this is happening, I get that the onClick function is clearing using a reference to the same instance of the array, and thus each iteration of the loop “changes” the array, rather than creating a new one, this is why in effect the last array value is used for all click events.

So the question is, what can I do to get the job done?

I thought of trying to “clone” the array inside the function the that didn’t work (I used .slice(0)) and probably rightly doesn’t work. Plus, I am guessing the it may even just be that the exact same function is being used for all 3 events.

  • 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-06-03T13:33:43+00:00Added an answer on June 3, 2026 at 1:33 pm

    Whenever this happens, its a scoping issue. You need to lock the array in to the onClick handler each time thru the array. You do that by creating a closure around it via a self invoking function.

    for (var i = 0; i < 3; i++) {
        var array = GetArray(i), onClick;
    
        // create a closure around onClick and array
        (function(array){
            onClick = function() {
                alert(array.length);
            }
        })(array);
    
        var html = "<a>click me</a><br/>";
        var element = $(html);
        $("div").append(element);
    
        element.click(onClick);
    }​
    

    Note this will alert 3, 0, 0, as the second and third time thru the leep you return an empty array.

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

Sidebar

Related Questions

I hope this is the appropriate title for this question. Right now I have
I want to call a function right after a scroll ends. I have tried
The title says all I want to know. Is there any function or I
I know a great thing about perl is that there's multiple ways of doing
We all know that CSS sprite images are great to reduce the amount of
i know nothing about medical records but im sure there's great opportunity in it
This is a great big mess... I know I could use an iframe, but
I have lists of articles made of: title, subtitle and body. Now I need
(The title is admittedly not that great. Please forgive my English, this is the
Ok the title isn't a great help I'm sorry but this is what I'm

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.