0

I don't use jquery much - but i have the need to fix some code in a "lighter" (and a bit older) UI for a larger solution.

It uses mix of various libraries - including jquery.

$("."+vehicleId+"_popupAddressCell").html(address);

is used to set the value for the field. It works - as long as its inside the:

success:{}

of a Ajax call. Right after the ajax call it fails. Logging it to console (without .html part) - shows that it didn't select anything - length 0. However i can navigate to that correct element in the children tree.

Why would it not select it, even tho it exist in its context-childNodes and only difference i see when i echo both is that... outside ajax it didn't select it. The vehicleId is not an ajax result nether is address ( instead of it it would be 'result') - those two variables exist in proper scope and are accessible.

updatePopup: function(vehicle) {
    var lat = vehicle.location()[0];
    var lon = vehicle.location()[1];
    var vehicleId = vehicle.id;
    var address = vehicle.address();
    Air.Ajax.request({
      url: 'Api/Objects/get',
      data: {
        lat: lat,
        lon: lon
      },
      success: function(response) {
        $("."+vehicleId+"_popupAddressCell").html(response);
        console.log($("."+vehicleId+"_popupAddressCell"));
      },
      scope: this
    });
    $("."+vehicleId+"_popupAddressCell").html(address);
    console.log($("."+vehicleId+"_popupAddressCell"));
  }

added above console.log test cases - the one inside success:

0: td.9880_popupAddressCell
context: document
length: 1
prevObject: x.fn.x.init[1]
selector: ".9880_popupAddressCell"

outside one would have:

context: document
length: 0
prevObject: x.fn.x.init[1]
selector: ".9880_popupAddressCell"
Mauro Tamm
  • 370
  • 2
  • 19
  • 2
    Can you post more of the function which calls the ajax? I can't see it being anything or than either a scoping issue of `vehicleId` or the element doesn't exist when the main function is run, i.e. it is created by the ajax result or something – Rhumborl Dec 05 '14 at 14:32
  • *After* the ajax call happens *before* the load completes. i.e before `success`. You are not taking into account the asynchronous nature of the Ajax call. – iCollect.it Ltd Dec 05 '14 at 14:32
  • Could we get some source code? It does sound like an async issue though where some html isn't being modified if it doesn't exist. – Orpheus Dec 05 '14 at 14:33
  • Without seeing the rest of the code, my guess is that you haven't waited for the DOM to load (domReady or equivalent) and when you run the selector immediately, the DOM hasn'nt been fully populated but in your Ajax call, when you come back later, the DOM is ready. – Kolban Dec 05 '14 at 14:33
  • And where do you define `vehicleId` ??? If you have posted all relevant code, i guess your issue would have been fixed in seconds – A. Wolff Dec 05 '14 at 14:36
  • As i stated the ´vehicleId´ and ´address´ do not come from ajax - they are simply defined as var before ajax call. Both callbacks get the same variables that show up at the ´selector:` field. – Mauro Tamm Dec 05 '14 at 14:50
  • Really a duplicate? this has NOTHING to do with getting response from ajax. Please read before you rush to abuse your powers. I can get data back from ajax just fine. – Mauro Tamm Dec 05 '14 at 15:05
  • Mauro, can you write in your code snippet where exactly you are trying to find the length of the element? In the success, or outside ajax? – Pulkit Mittal Dec 05 '14 at 15:06
  • I am not trying to find the length. when i simply use ´console.log($("."+vehicleId+"_popupAddressCell"));´ to test inside and outside success - only the inside has successfully selected that element. – Mauro Tamm Dec 05 '14 at 15:08
  • Added console.log test cases to show the difference. – Mauro Tamm Dec 05 '14 at 15:13
  • Is this element `.9880_popupAddressCell` available when the function updatePopup is called? My guess is it might be created dynamically and it takes a few milliseconds to show up on DOM and by the time the AJAX is completed, it is available. – Pulkit Mittal Dec 05 '14 at 15:22

0 Answers0