views:

202

answers:

7

Could someone explain why the alert statement would return nothing when used in this context:

$(document).ready(function(){

    $.get("/getsomedata.php", function(data){
        $("#mydiv").append(data)     
      });

     alert($("#mydiv").html()); //outputs nothing

});

When this statement returns what you would expect:

$(document).ready(function(){

     $("#mydiv").append('some info')     
      alert($("#mydiv").html()); //outputs 'someinfo'

});
+6  A: 

the .get isn't blocking, the rest of your script continues to run while the document loads. To get it to work as you expect, put the alert inside the anonymous function you are providing. This is a callback function and will fire after the document is loaded. ie:

$(document).ready(function(){

$.get("/getsomedata.php", function(data){
    $("#mydiv").append(data);
    alert($("#mydiv").html()); // This won't fire til somedata.php is loaded
  });

});
Erik
That's helpful, so what would the correct way to do this? I need to make sure the data is back in the page as the rest of my script depends on this.
Henry
See my edit above; if you do something in the call back, it wont happen until AFTER the document is loaded.
Erik
Include any code that depends on the asynchronous call in the callback function. You should think about what would happen if the php page has an error, is slow, or doesn't provide the information you're after.
S Pangborn
Top job, that answers this perfectly. Many thanks!
Henry
+3  A: 

The data is not back yet from the asynchronous call. function(data){$("#mydiv").append(data)} is the callback function, and executes when the data comes back. Nothing waits for that. That function will happen when the data comes back, but this will almost certainly be some milliseconds after 'alert' call has happened.

For any functionality dependent on that data coming back, have that functionality happen in the callback function itself. This way it will wait for the data to come back, but will happen as soon as possible.

Something like:

$(document).ready(function(){

    $.get("/getsomedata.php", function(data){
        $("#mydiv").append(data);
        alert($("#mydiv").html()); // will show your data     
      });
});
Patrick Karcher
+3  A: 

AJAX calls are asynchronous (by default), which means that your alert is called before the AJAX request completes (or your callback is executed) and the element which's html you are trying to alert is empty at that moment.

If you want to solve your problem just put the code that deals with the HTML of that DIV inside your callback function that you have in your $.get()

Jan Hančič
+1  A: 

AJAX is asynchronous, meaning that the append is happening AFTER the alert. The function passed to $.get() is a "callback" as in - gets called at a later time when the operation has finished. Any manipulation of the data you receive should be handled in the callback:

$(document).ready(function(){
  $.get("/getsomedata.php", function(data){
    // this is a "callback"
    $("#mydiv").append(data)     
    alert($("#mydiv").html());
  });
});
gnarf
A: 

You call get method and this is a function that calls the XMLHttpRequest asynchronously -

andres descalzo
+2  A: 
$(document).ready(function(){

$.get("/getsomedata.php", function(data){
    $("#mydiv").append(data)     
    alert($("#mydiv").html()); //outputs nothing
});


});

Write it like this. Otherwise like your example the html is alerted before the GET request is done.

Joe
A: 

That's because the the alert is called first, and the callback that fill the div is finished later, and ocours asynchronously

Israel Rodriguez