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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:07:24+00:00 2026-05-23T15:07:24+00:00

When I get some json data I want to display them in the page

  • 0

When I get some json data I want to display them in the page :

code :

    function update(){

      for(far i=0;i<data.length;i++){
        var l=document.createElement("li"); 
        document.getElementById("ul_con").appendChild(l);
        l.onclick=function(){
          alert ("id:"+data[i].id);
        }

      }
}

However,I get the error: data[i]i is not defined.

any way?

BTW,since I post this question on my phone,so I can not format the code well.l hope someone can do me a favor.

UPDATE:

    function update(){

      for(var i=0;i<data.length;i++){
        var l=document.createElement("li"); 
        l.tindex=i;
        document.getElementById("ul_con").appendChild(l);
        l.onclick=function(){
          alert ("id:"+data[this.tindex].id);
        }

      }
}

This code works also,but I wonder why the “data” can be used when the click event occur while the “i” can not?

  • 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-23T15:07:25+00:00Added an answer on May 23, 2026 at 3:07 pm

    When the onclick function is called, it is at a much later time than when you hooked up the event handler and the data and i values may no longer be what they were at the moment you hooked up the function so you cannot rely on them. In fact, the i value is guaranteed not to be the same because it will have advanced because of subsequent interations of the loop. The data value may or may not be valid depending upon the rest of your code.

    You can freeze them for use within the event handler like this:

    function update(){
        for(far i=0;i<data.length;i++){
            var l=document.createElement("li"); 
            document.getElementById("ul_con").appendChild(l);
            l.onclick=(function(mydata, index){     // define function that gets called at assignment time
                // variables mydata and index are available here in this scope
                 return function() {
                    alert ("id:"+mydata[index].id);
                };
           })(data, i);   // pass data and i into the inner scope
        }
    }
    

    By doing it this way, the first function(data,i) is executed at the time the onclick handler is assigned. This evaluates data and i and establishes their value for the use in the inner function via the function closure. See this writeup for a description and more examples. Since data is an array, you are not making a copy of it here (it is passed by reference) so you will have to make sure that its values are preserved until the time of the click handler (though it will not go out of scope because this closure preserves it). The value of i is copied in the function parameter so you don’t have to worry about the outer loop changing the value of i.

    It’s a little less confusing and more easily understandable when it looks like this, but if you study the two, you can see that they are really the same thing, the first example just has an inline nameless function definition:

    function processMyClick(mydata, index) {
        return function() {
            alert ("id:"+mydata[index].id);
        };
    }
    
    function update(){
        for(far i=0;i<data.length;i++){
            var l=document.createElement("li"); 
            document.getElementById("ul_con").appendChild(l);
            l.onclick=processMyClick(data, i);
        }
    }
    

    If all you need is a couple pieces of data out of that data structure, then you may want to just put it on the DOM object itself like this:

    function update(){
        for(far i=0;i<data.length;i++){
            var l=document.createElement("li"); 
            document.getElementById("ul_con").appendChild(l);
            l.myId = data[i];
            l.onclick=function() {alert(this.myId);};
        }
    }
    

    When putting data on DOM objects, you do have to be careful about cleaning up appropriately to avoid potential memory leaks or circular references if you create/destroy a lot of these and run in older browsers.

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

Sidebar

Related Questions

I have a website where an ajax call will get some Json data from
I have some JSON data that I get from a server. In my JavaScript,
I want to fetch data from the webpage, that contains some json data. The
I want to make a PHP JSON data foreach, but I met some problem.
I want to render some json data using HTML template. I haven't started implementing
i have some json data, and i want to convert it into plain text,
I am trying to get some errors returned in JSON format. So, I made
To get some smooth graphics, I want to draw oversampled by factor 2 and
I am trying to get some XML data with LINQ, but running into a
I'm trying to get some code working that a previous developer has written. Yep,

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.