Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Jquery append() behave asynchronously?

I've seen a number of posts (append supposedly immediate) with conflicting accepted answers on this. We're using JQuery 1.4 (http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js) and append() seems to be asynchronous, such that:

Edited to show code in context of AJAX callback

 ...  var message = $.ajax({    type: "GET",    url: "/getVolumes/" +  _Id,    async: false   }).responseText;  if (parseInt(message) != 0){    var $results = $(message);    $MAIN_DIV.append($results);    retrieveTargets();  } ...     function retrieveTargets(){   var $targets = $(".resultTargets"); } 

Executes and creates the page as expected, yet the targets query yields nothing at runtime. Running the same code in the JS console retrieves the elements as expected.

If this is the expected behavior in JQuery what's the proper way to wait until append is finished?

like image 938
RSG Avatar asked Feb 22 '11 23:02

RSG


People also ask

Is jQuery function asynchronous?

The jQuery Ajax async is handling Asynchronous HTTP requests in the element. It is a procedure to send a request to the server without interruption. It is an Asynchronous method to send HTTP requests without waiting response. It is a function to working on a server without associating more than on request.

Is jQuery post asynchronous?

The jQuery post() method sends asynchronous http POST request to the server to submit the data to the server and get the response. Syntax: $. post(url,[data],[callback],[type]);

What is jQuery append?

jQuery append() Method The append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.

Is jQuery synchronous?

Synchronous AJAX call is made when async setting of jQuery AJAX function is set to false while Asynchronous AJAX call is made when async setting of jQuery AJAX function is set to true. Default value of the async setting of jQuery AJAX function is true.


1 Answers

All of the comments helped track this down. I was following a red herring in the console. The problem wasn't with synchronicity, it was with the next lines:

$targets.each( function(){   ...   this.html();   ... 

Needed to be

$(this).html(); 

In short, everyone was correct. Jquery append() behaves synchronously.

like image 73
RSG Avatar answered Sep 21 '22 13:09

RSG