{"id":158,"date":"2010-04-12T21:49:04","date_gmt":"2010-04-13T04:49:04","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=158"},"modified":"2010-04-12T21:49:04","modified_gmt":"2010-04-13T04:49:04","slug":"jquery-difference-between-append-and-prepend","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2010\/04\/12\/jquery-difference-between-append-and-prepend\/","title":{"rendered":"JQuery &#8211; difference between append() and prepend()"},"content":{"rendered":"<p><strong><code>append()<\/code><\/strong> and <strong><code>prepend()<\/code><\/strong> methods: similar to appendChild, except jQuery accepts a string, a DOM element, or a jQuery object as valid arguments. jQuery converts the argument into a text node or element depending on what is passed.<\/p>\n<ol>\n<li><code>$(\"#leftNav ul\").append(\"&lt;li&gt;after the last &amp;lt;li&amp;gt;&lt;\/li&gt;\"); \/\/ adds  &lt;li&gt; at the end of  &lt;ul&gt;<\/code><\/li>\n<li><code>$(\"#leftNav ul\").prepend(\"&lt;li&gt;before the first &amp;lt;li&amp;gt;&lt;\/li&gt;\"); \/\/ adds &lt;li&gt; at the start of &lt;ul&gt;<\/code><\/li>\n<li><code>$(\"#leftNav ul\").append($(\"#mainContent h2\")); \/\/ moves headers from the mainContent to the leftNav<\/code><\/li>\n<li><code><br \/>\n$(\"#leftNav ul\").append($(\"#mainContent h2\").html()); \/\/copies  text of the first h2, adds it to leftNav<\/code><\/li>\n<\/ol>\n<p>There are two similar methods \u2014 <code>appendTo()<\/code> and <code>prependTo()<\/code> \u2014 which are basically inververted verstions of <code>append()<\/code> and <code>prepend()<\/code>. <code>A.append(B)<\/code> is about equal to <code> B.appendTo(A)<\/code><\/p>\n<p>Note: As shown in example 3, if you try to append an element that already exists in the DOM, that element will first be removed from DOM before being reinserted, or appended, in the new location.<\/p>\n<p><code><strong>before()<\/strong><\/code> and <code><strong>after()<\/strong><\/code> methods: similar to <code>insertBefore()<\/code> method (and the non-existant, but found in many javascript libraries, includeing this one, <code>insertAfter()<\/code>). The <code>after()<\/code> method  inserts a node after a selected one. The <code>before()<\/code> method   inserts a node before a selected one, like the javascript standard method of  <code>insertBefore()<\/code>.<\/p>\n<ol>\n<li><code>$(\"#leftNav ul\").before(\"&lt;h2&gt;Left Navigation&lt;\/h2&gt;\"); \/\/ adds  header before the &lt;ul&gt;<\/code><\/li>\n<li><code>$(\"#leftNav ul\").after(\"&lt;h4&gt;New Header&lt;\/h4&gt;\"); \/\/ adds  header after the &lt;ul&gt;<\/code><\/li>\n<li><code>$(\"#leftNav h4\").before($(\"#mainContent h2\").html()); \/\/copies text of the first h2, adds it to leftNav before our new &lt;h4&gt;<\/code><\/li>\n<\/ol>\n<p>There are two similar methods \u2014 <code>insertBefore()<\/code> and <code>insertAfter()<\/code> \u2014 which are basically inververted versions of <code>before()<\/code> and <code>after()<\/code>. <code>A.before(B)<\/code> is about equal to <code> B.insertBefore(A)<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>append() and prepend() methods: similar to appendChild, except jQuery accepts a string, a DOM element, or a jQuery object as valid arguments. jQuery converts the <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2010\/04\/12\/jquery-difference-between-append-and-prepend\/\" title=\"JQuery &#8211; difference between append() and prepend()\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[36,40,97,162,210,257],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-amit","tag-append","tag-difference","tag-jquery","tag-prepend","tag-truelogic"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":0,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}