difference between .append() and .appendTo()

There is no difference between .append() and .appendTo() functions except the syntax:

1
2
3
$('.target').append('text');
 
$('text').appendTo('.target');

But they both needed because of chaining and code readability:

1
2
3
4
5
6
7
8
9
10
11
12
13
$(function(){
 
    $('.target').append('<div class="element well">.element</div>')
 
        .css('background-color', 'yellow'); // yellow collor added to .target
 
     
 
    $('<div class="element2 well">.element2</div>').appendTo('.target2')
 
        .css('background-color', 'yellow'); // yellow collor added to .element2
 
});​

html:

1
2
3
4
5
6
7
8
9
10
11
12
13
<div class="well target">
 
    <span class="label label-info">.append() example</span> .target
 
</div>
 
 
 
<div class="well target2">
 
    <span class="label label-info">.appendTo() example</span> .target2
 
</div>

The same difference applies to:

Leave a Comment