Using jquery $ .get in a finished document

Can someone explain why the warning statement does not return anything 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 expect:

$(document).ready(function(){

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

});
+3
source share
7 answers

.get does not block, the rest of your script continues to run while the document is loading. To make it work the way you expect, put a warning inside the anonymous function that you provide. This is a callback function and will run after loading a document. i.e:

$(document).ready(function(){

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

});
+6
source

. function(data){$("#mydiv").append(data)} - , . . , , , "".

, , . , , .

- :

$(document).ready(function(){

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

AJAX- ( ), , AJAX ( ), , html, , .

, , HTML DIV , $.get()

+3
$(document).ready(function(){

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


});

. , , html GET.

+2

AJAX , , . , $.get(), " ", - , . , , :

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

get, , XMLHttpRequest -

0

, , , div, , ocurs

0

Source: https://habr.com/ru/post/1727675/


All Articles