Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery function after .append

Tags:

jquery

How to call a function after jQuery .append is completely done?

Here's an example:

$("#root").append(child, function(){    // Action after append is completly done }); 

The issue: When a complex DOM structure is appended, calculation of new size of the root element within the append function callback is wrong. Assumptions are that the DOM in still not completely loaded, only the first child of the added complex DOM is.

like image 371
David Horák Avatar asked May 20 '11 07:05

David Horák


People also ask

How to add after div in jQuery?

jQuery insertAfter() Method The insertAfter() method inserts HTML elements after the selected elements. Tip: To insert HTML elements before the selected elements, use the insertBefore() method.

How to add function in append jQuery?

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.

How to after in jQuery?

The after() is an inbuilt function in jQuery which is used to insert content, specified by the parameter for the each selected element in the set of matched element. Syntax: $(selector). after(A);

How to use appendchild in jQuery?

The . append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use . prepend() ). The .


1 Answers

You've got many valid answers in here but none of them really tells you why it works as it does.

In JavaScript commands are executed one at a time, synchronously in the order they come, unless you explicitly tell them to be asynchronous by using a timeout or interval.

This means that your .append method will be executed and nothing else (disregarding any potential timeouts or intervals that may exist) will execute until that method have finished its job.

To summarize, there's no need for a callback since .append will be run synchronously.

like image 184
mekwall Avatar answered Sep 19 '22 15:09

mekwall