There is no big difference between those functions except the syntax:
$('.target').append('text');
$('text').appendTo('.target');
As said in jQuery docs:
The .append() and .appendTo() methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With .append(), the selector expression preceding the method is the container into which the content is inserted. With .appendTo(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.
So in which case it is better to use the .append(), and which .appendTo()? In which code-samples fit only one of those two functions and the other is not good enough?
The same question applies to:
$('.target').append('<div>Hello</div>').hide()
will hide the.target
elements after appending the div.$('<div>Hello</div>').appendTo('.target').hide()
will hide the div after appending it to.target
. – Pretoria