The difference between append () and Preappend (), after () and before () in jquery
append () and prepend ()
Assume
<div class=‘a‘> //<---you want div c to append in this <div class=‘b‘>b</div></div>
Use
$(‘.a‘).append($(‘.c‘));
This will be the case:
<div class=‘a‘> //<---you want div c to append in this <div class=‘b‘>b</div> <div class=‘c‘>c</div></div>
Use
$(‘.a‘).prepend($(‘.c‘));
The result is this:
<div class=‘a‘> //<---you want div c to append in this <div class=‘c‘>c</div> <div class=‘b‘>b</div></div>
After () and before ()
The same code above, using the
$(‘.a‘).after($(‘.c‘));
Results:
<div class=‘a‘> <div class=‘b‘>b</div></div><div class=‘c‘>c</div> //<----this will be placed here
Use
$(‘.a‘).before($(‘.c‘));
Results:
<div class=‘c‘>c</div> //<----this will be placed here<div class=‘a‘> <div class=‘b‘>b</div></div>
From the above we can draw the conclusion that:
Append () & Prepend () inserts content inside the element (the content becomes a child element or node of the element), after () & before () is the insertion of content outside the element (the sibling node whose content becomes an element).
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The difference between append () and prepend (), after () and before () in jquery