jQuery .append() .appendTo() method

0 votes
277 views
added Mar 20, 2018 in jQuery by LC Marshal Captain (25,790 points)
edited Apr 2, 2018 by LC Marshal

.appendTo( target ) - Insert every element in the set of matched elements to the end of the target.

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. src: http://api.jquery.com/appendto/

Example

Find .content and hide it first prior insert or add it to the end of #mylastdiv

$('.content').hide();
$('.content').appendTo("#mycontent").fadeIn(1000);

1 Response

0 votes
responded Apr 13, 2018 by LC Marshal Captain (25,790 points)

Find .submitted and insert it under h1.page-header (in its class)

$('span.submitted').appendTo('h1.page-header').fadeIn(400);

 

Find .submitted and insert it before h1.page-header (in its class)

$('span.submitted').prependTo('h1.page-header').fadeIn(400);
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...