.append(), prepend(), .after() and .before()

346,367

Solution 1

See:


.append() puts data inside an element at last index and
.prepend() puts the prepending elem at first index


suppose:

<div class='a'> //<---you want div c to append in this
  <div class='b'>b</div>
</div>

when .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>

Fiddle with .append() in execution.


when .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>

Fiddle with .prepend() in execution.


.after() puts the element after the element
.before() puts the element before the element


using after:

$('.a').after($('.c'));

after execution:

<div class='a'>
  <div class='b'>b</div>
</div>
<div class='c'>c</div> //<----this will be placed here

Fiddle with .after() in execution.


using before:

$('.a').before($('.c'));

after execution:

<div class='c'>c</div> //<----this will be placed here
<div class='a'>
  <div class='b'>b</div>
</div>

Fiddle with .before() in execution.


Solution 2

This image displayed below gives a clear understanding and shows the exact difference between .append(), .prepend(), .after() and .before()

jQuery infographic

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:

jQuery insertion infographic, plus flipped versions of the 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.

Solution 3

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).

Solution 4

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.

Solution 5

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

Share:
346,367

Related videos on Youtube

Epik
Author by

Epik

Delete me

Updated on March 09, 2020

Comments

  • Epik
    Epik about 4 years

    I am pretty proficient with coding, but now and then I come across code that seems to do basically the same thing. My main question here is, why would you use .append() rather then .after() or vice verses?

    I have been looking and cannot seem to find a clear definition of the differences between the two and when to use them and when not to.

    What are the benefits of one over the other and also why would i use one rather then the other?? Can someone please explain this to me?

    var txt = $('#' + id + ' span:first').html();
    $('#' + id + ' a.append').live('click', function (e) {
        e.preventDefault();
        $('#' + id + ' .innerDiv').append(txt);
    });
    $('#' + id + ' a.prepend').live('click', function (e) {
        e.preventDefault();
        $('#' + id + ' .innerDiv').prepend(txt);
    });
    $('#' + id + ' a.after').live('click', function (e) {
        e.preventDefault();
        $('#' + id + ' .innerDiv').after(txt);
    });
    $('#' + id + ' a.before').live('click', function (e) {
        e.preventDefault();
        $('#' + id + ' .innerDiv').before(txt);
    });
    
    • mikakun
      mikakun over 11 years
      i never use append which (use to?) bugs ie, i use appendTo that seems alsomore correct semantically especially since there's after (chain chain chain) - nb: after is after not within
    • tgoneil
      tgoneil over 6 years
      Sounds like what you really want to know is why to choose append vs after to achieve the same result. Say you have <div class='a'><p class='b'></p></div>. Then $('.a').append ('hello') would have the same affect as $('.b').after('hello'). Namely: <div class='a'><p class='b'></p>'hello'</div>. In that case, it doesn't matter. The resulting html is the same, so choose append or after depending on what selector is most convenient to construct in your code.
  • Jai
    Jai about 10 years
    @joraid updated the answer as per your comment with some js fiddle links in execution.
  • Djeroen
    Djeroen over 8 years
    great explanation, but what if i want to load in an external html view? Using MVC like so: $("#viewPlaceHolder").append("/clients/RelationDropdown", {selected: selected });
  • Jai
    Jai over 8 years
    @Djeroen .append() will not load it that way. You have to use .load(url, params).
  • Apostolos
    Apostolos over 6 years
    Nice! Can I also ask about the difference between '$(element).append(content)' and '$(content).appendTo(target)', and the like? (I don't want to start a new thread in this!)
  • Abdul Hameed
    Abdul Hameed over 6 years
    very easily understandable picture. :)
  • Admin
    Admin about 6 years
    Upvoted! If I ask then what about the appendTo and prependTo in picture? Please update.
  • Bob Stein
    Bob Stein over 5 years
    I keep referring to your infographic. Hope you like the addition of the flipped functions.