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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:18:05+00:00 2026-05-18T23:18:05+00:00

To put things in context, I’m loading a list of items via Ajax, creating

  • 0

To put things in context, I’m loading a list of items via Ajax, creating a div with main info for each one and want to display details on page when clicking on it. So I have that code in my onSuccess :

items = transport.responseText.evalJSON(); // my list of objects that contains all the details I'll need for that page
for (var itemID in items)
{
    newDiv = ... // Creating my div with main infos
    $('myDiv').appendChild(newDiv);

    // More code to make everything look pretty and that works fine

    Event.observe(newDiv, 'click', function() { loadItem(itemID); });
}

loadItem is my function that will display all the item details. And my problem is that itemID isn’t replace by its value when creating the observe event, so it always returns the same ID for all items.

Any idea how I can fix that ? I checked bind on prototype doc, that seemed to be made for those cases, but probably didn’t get it, since it wouldn’t work for me.

  • 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-18T23:18:06+00:00Added an answer on May 18, 2026 at 11:18 pm

    For a minimal-impact fix, replace your Event.observe line with this:

    Event.observe(newDiv, 'click', loadItem.curry(itemID));
    

    Explanation:

    In your original code, the event handler functions you’re creating close over (have an enduring reference to) the itemID variable, and so will use the value of that variable at of when the event handler is called, not as of when you assign it to the event. That value will be the last value that itemID has in the loop — for all of the handler functions. More about closures here.

    With the minimal-impact revised code, we use Prototype’s curry function, which will create a function for you that, when called, will call the underlying function with the arguments you gave curry. (The name is from mathematics; Haskell Curry came up with the technique, though there are arguments he wasn’t the first to do so.) We could do the same thing ourselves:

    items = transport.responseText.evalJSON(); // my list of objects that contains all the details I'll need for that page
    for (var itemID in items)
    {
        newDiv = ... // Creating my div with main infos
        $('myDiv').appendChild(newDiv);
    
        // More code to make everything look pretty and that works fine
    
        Event.observe(newDiv, 'click', prepLoadItem(itemID));
    }
    
    function prepLoadItem(id) {
        return function() {
            loadItem(id);
        };
    }
    

    …but because Prototype has a general-purpose function for it, we don’t have to.

    Off-topic: Is items an array? If not, ignore this off-topic comment. If so, don’t use for..in to loop through it, or at least, not unless you take some precautions the code above doesn’t to do it properly. Details here, but for..in is not for looping through the indexes of an array; it’s for looping through the properties of an object. Array objects may well have properties other than array indexes (and in fact, if you’re using Prototype, they do.)

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

Sidebar

Related Questions

There are a few things that I almost always do when I put a
Simply put, I have a table with, among other things, a column for timestamps.
We put common prefixes on related tables to assure they display next to each
Put differently: Is there a good reason to choose a loosely-typed collection over a
Put it another way: what code have you written that cannot fail. I'm interested
We put all of our unit tests in their own projects. We find that
I put better in quotes because it's a qualitative question. I've been writing COM
Simply put, is there a way to create a 2D javascript array using similar
To put it simple, there's a simple java swing app that consists of JFrame
Simply put, MVC is the pattern for separating contents (model) from presentation (view), and

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.