.append()
puts data inside an element at last index
and.prepend()
puts the prepending elem at first index
<div class='a'> //<---you want div c to append in this
<div class='b'>b</div>
</div>
.append()
executes it will look like this:$('.a').append($('.c'));
after execution:
<div class='a'> //<---you want div c to append in this
<div class='b'>b</div>
<div class='c'>c</div>
</div>
.prepend()
executes it will look like this:$('.a').prepend($('.c'));
after execution:
<div class='a'> //<---you want div c to append in this
<div class='c'>c</div>
<div class='b'>b</div>
</div>
.after()
puts the element after the element.before()
puts the element before the element
$('.a').after($('.c'));
after execution:
<div class='a'>
<div class='b'>b</div>
</div>
<div class='c'>c</div> //<----this will be placed here
$('.a').before($('.c'));
after execution:
<div class='c'>c</div> //<----this will be placed here
<div class='a'>
<div class='b'>b</div>
</div>
This image displayed below gives a clear understanding and shows the exact difference between .append()
, .prepend()
, .after()
and .before()
You can see from the image that .append()
and .prepend()
adds the new elements as child elements (brown colored) to the target.
And .after()
and .before()
adds the new elements as sibling elements (black colored) to the target.
Here is a DEMO for better understanding.
EDIT: the flipped versions of those functions:
Using this code:
var $target = $('.target');
$target.append('<div class="child">1. append</div>');
$target.prepend('<div class="child">2. prepend</div>');
$target.before('<div class="sibling">3. before</div>');
$target.after('<div class="sibling">4. after</div>');
$('<div class="child flipped">or appendTo</div>').appendTo($target);
$('<div class="child flipped">or prependTo</div>').prependTo($target);
$('<div class="sibling flipped">or insertBefore</div>').insertBefore($target);
$('<div class="sibling flipped">or insertAfter</div>').insertAfter($target);
on this target:
<div class="target">
This is the target div to which new elements are associated using jQuery
</div>
So although these functions flip the parameter order, each creates the same element nesting:
var $div = $('<div>').append($('<img>'));
var $img = $('<img>').appendTo($('<div>'))
...but they return a different element. This matters for method chaining.
append()
& prepend()
are for inserting content inside an element (making the content its child) while after()
& before()
insert content outside an element (making the content its sibling).
The best way is going to documentation.
.append()
vs .after()
append()
: Insert content, specified by the parameter, to the end of each element in the set of matched elements.after()
: Insert content, specified by the parameter, after each element in the set of matched elements. .prepend()
vs .before()
prepend()
: Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.before()
: Insert content, specified by the parameter, before each element in the set of matched elements.So, append and prepend refers to child of the object whereas after and before refers to sibling of the the object.
There is a basic difference between .append()
and .after()
and .prepend()
and .before()
.
.append()
adds the parameter element inside the selector element's tag at the very end whereas the .after()
adds the parameter element after the element's tag.
The vice-versa is for .prepend()
and .before()
.
Fiddle
There is no extra advantage for each of them. It totally depends on your scenario. Code below shows their difference.
Before inserts your html here
<div id="mainTabsDiv">
Prepend inserts your html here
<div id="homeTabDiv">
<span>
Home
</span>
</div>
<div id="aboutUsTabDiv">
<span>
About Us
</span>
</div>
<div id="contactUsTabDiv">
<span>
Contact Us
</span>
</div>
Append inserts your html here
</div>
After inserts your html here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With