views:

46

answers:

2

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
+3  A: 
T.J. Crowder
TJ: Thanks for the VERY thorough explanation. I guess I should have realized this do to the async nature of events. Thanks for the help!
CoryDorning
and yes, this is all in a function
CoryDorning
A: 

The problem will probably be that when the AJAX callback is called $currEl will be set to a different value, or null, as it will take the value of $currEl when the callback is called, not when the AJAX function is declared.

Fermin