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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:13:01+00:00 2026-06-08T18:13:01+00:00

I have a function which loops an array… whilst it works it seems to

  • 0

I have a function which loops an array… whilst it works it seems to change the value of the information being sent from “begin” function to “process” function, but i don’t know why… I’m sure i made a silly mistake but I cannot see the mistake =/

This is my function:

var array_data = data[9]; //global array to use
console.log(array_data); //debug

function process(i){
alert('Number is '+i); // shows the value "7" (should show value "1")
}

function begin(){
var count = 0;

for(i in array_data){

if(parseInt(array_data[i][9])){ //if true

    var result = create_layout(i); //function to make the layout
    alert('Number is '+i); //shows the value "1" (this is correct so far)

   document.getElementById('result'+count).innerHTML = result;  
   document.getElementById('result'+count).onclick = function() { process(i); };    

count++;

       }
}   

window.onload = function() {  
begin();  
};

Below is my array for (array_data) from the console log:

1: Array[10]
   0: "Car One"
   1: "1"
   2: "3"
   3: "d2.jpg"
   4: "1"
   5: "1"
   6: "200"
   7: "85"
   8: "5000"
   9: "1"
length: 10    

7: Array[10]
   0: "Car Two"
   1: "1"
   2: "1"
   3: "e2.jpg"
   4: "1"
   5: "0"
   6: "500"
   7: "50"
   8: "3000"
   9: "0"
length: 10

So I’m wondering why might it is changing the value of “i” when it reaches the process function ?

  • 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-08T18:13:02+00:00Added an answer on June 8, 2026 at 6:13 pm

    By the time the onclick function actually gets called, the value of i will have changed because of the loop it’s in. You should “anchor” its value. The easiest way to do it is like this:

    for( some loop on `i`) {
        (function(i) {
            // your code that depends on `i`
        })(i);
    }
    

    This will ensure that the value of i will not change within that closure (unless you change it yourself)

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

Sidebar

Related Questions

I have a function which performs a foreach loop on an array from a
i have a function which retrieves values from a webservice , then loops through
I have a for loop which loops through an array and want to match
I have a php script with an array which loops through months and displays
I have the following code, which loops through an array of menuoptions and on
I have a function which looks something like this, it returns a noncopyable class
Suppose I have a function which looks like this: template <class In, class In2>
I have a function which I have to loop it a few times but
I have a for-loop which performs the following function: Take a M by 8
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(

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.