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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:50:00+00:00 2026-06-11T13:50:00+00:00

Possible Duplicate: Event handlers inside a Javascript loop – need a closure? I am

  • 0

Possible Duplicate:
Event handlers inside a Javascript loop – need a closure?

I am getting some json object through an API call.It is of the folowing format:-

{data:[{id:"1",name:"some1",url:"someurl1"},{id:"2",name:"some2",url:"someurl2"}...]}

I get it through a jsonp result and it is parsed as below:-

function(results){
   for(var i=0;i<10;i++){
    item=document.createElement("div");
    item.innerHTML=results.data[i].name;
    item.onclick=function(){
        console.log(results.data[i]);  //--->this is where i am stuck 
      }
    }
 }

How do i pass the particular object from the loop to the onclick event. I mean the first div created should have an onclick event with the data of the first object and the second div should have the data from the second object..
Please ask me if any more clarification is required

Edit:-
If I do something like this:-

item.onclick=function(){
  console.log(results.data[1])
}

I get that particular object in the onclick event of all the items , but that is not what i want

Edit:-
This is how i finally solved it. Thanks to the link pointed to by DCoder.

item.onclick =function(object){
           return function(){
           //do something with the object
           }

         }(obj);
  • 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-11T13:50:02+00:00Added an answer on June 11, 2026 at 1:50 pm

    Quick solution:

    function(results){
        for(var i=0;i<10;i++){
            (function(index){ // create a closure, this makes a new scope
                item=document.createElement("div");
                item.innerHTML=results.data[index].name;
                item.onclick=function(){
                    console.log(results.data[index]);  //--->this is where i am stuck 
                }
            })(i);  // pass in i
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I have
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I want
Possible Duplicate: Javascript closure inside loops - simple practical example I add event handlers
Possible Duplicate: JavaScript closures and variable scope Assign click handlers in for loop I
Possible Duplicate: Remove handlers on disposing object I have some code which dynamically adds
Possible Duplicate: JavaScript Event Listeners vs Event Handlers element.onload vs element.addEventListener(“load”,callbak,false) I've read this
Possible Duplicate: Need to cancel click/mouseup events when double-click event detected Preventing double-click of
Possible Duplicate: Getting the ID of the element that fired an event using JQuery
Possible Duplicate: “On file dialog cancel” event in JavaScript Is there a listener for
Possible Duplicate: Inspect attached event handlers for any DOM element Is there a tool

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.