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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:03:38+00:00 2026-05-17T19:03:38+00:00

I have the following code, which i’ve simplified, where $currEl gets logged and displays

  • 0

I have the following code, which i’ve simplified, where $currEl gets logged and displays correctly, but in the $.ajax call it logs as null.

Am I missing something?

for(var i=0, j = atgSlots.length; i < j; i++) {
 var currSlot = atgSlots[i].split('|'),
   $currEl = currSlot[0].length ? $('[data-atg-url=' + currSlot[0] + ']') : null,
   wcmLocation = currSlot[2] || null;

 if ($currEl !== null && wcmLocation !== null) {
  console.log($currEl);
  $.ajax({
   url: wcmLocation,
   success: function(html) { console.log($currEl); updateSlots.setContent($currEl, html); },
   error: updateSlots.checkDefault
  }); // $.ajax
 }
} // for : atgSlots
  • 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-17T19:03:38+00:00Added an answer on May 17, 2026 at 7:03 pm

    The problem is that your ajax success function has a live reference to the $currEl variable, not a copy of it as of when the function was created. Consequently, all of those success handlers are referencing the same variable and therefore the same value — the last value assigned to $currEl in the loop. Here’s a much, much simplified example of the same effect:

    var a;
    a = "before";
    setTimeout(function() {
        alert(a);
    }, 10);
    a = "after";
    

    That alerts “after”, not “before”, because the function that gets called after 10ms uses the current value of a. See it in action.

    You can address this by giving each function its own variable to refer to, using an intermediary function. Here’s a minimalist change:

    for(var i=0, j = atgSlots.length; i < j; i++) {
     var currSlot = atgSlots[i].split('|'),
       $currEl = currSlot[0].length ? $('[data-atg-url=' + currSlot[0] + ']') : null,
       wcmLocation = currSlot[2] || null;
    
     if ($currEl !== null && wcmLocation !== null) {
      console.log($currEl);
      $.ajax({
       url: wcmLocation,
       // ==== Change starts here
       success: (function($thisEl) {
         return function(html) { console.log($thisEl); updateSlots.setContent($thisEl, html); };
       })($currEl),
       // ==== Change ends here
       error: updateSlots.checkDefault
      }); // $.ajax
     }
    } // for : atgSlots
    

    What that does is create and call a factory function on each loop, passing into the function the current value of $currEl. That function then returns the function that should be used as the success handler. The success handler uses other information from the outer context (html, which I’m assuming should be common to all of them), but uses $thisEl (the function argument) instead of $currEl.

    It’s actually slightly inefficient to do it that way, because we create multiple identical copies of our new factory function. A less minimalist — and perhaps clearer — version would look like this:

    for(var i=0, j = atgSlots.length; i < j; i++) {
     var currSlot = atgSlots[i].split('|'),
       $currEl = currSlot[0].length ? $('[data-atg-url=' + currSlot[0] + ']') : null,
       wcmLocation = currSlot[2] || null;
    
     if ($currEl !== null && wcmLocation !== null) {
      console.log($currEl);
      $.ajax({
       url: wcmLocation,
       success: buildSuccessHandler($currEl),
       error: updateSlots.checkDefault
      }); // $.ajax
     }
    } // for : atgSlots
    
    function buildSuccessHandler($thisEl) {
        return function(html) {
            console.log($thisEl);
            updateSlots.setContent($thisEl, html);
        };
    }
    

    (I’m assuming all of this code is contained inside a function somewhere.)

    More about closures and this live reference thing in this blog post: “Closures are not complicated”.

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

Sidebar

Related Questions

I have following code which works fine in firefox and ie 8, but in
I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
Okay so I have the following Code which appends a string to another in
I have the following JQuery code which does similar functionality like Stackoverflow where the
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have the following JavaScript code: Link In which the function makewindows does not
I have the following code, which will not work. The javascript gives no errors

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.