{"id":1456,"date":"2012-11-06T10:13:32","date_gmt":"2012-11-06T10:13:32","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=1456"},"modified":"2012-11-06T10:13:32","modified_gmt":"2012-11-06T10:13:32","slug":"jquery-appendto-using-appendto-in-jquery-appendto-method-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-appendto-using-appendto-in-jquery-appendto-method-examples\/","title":{"rendered":"jQuery appendTo Examples"},"content":{"rendered":"<p>Using <em><strong>jQuery appendTo method<\/strong><\/em>, you can easily add content, dynamic or static. This is immensely helpful with inserting dynamic content in a div or any other element. In this article, I am going to share info on the appendTo() method along with easy to follow examples.<!--more--><\/p>\n<h2>How to use jQuery appendTo method<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1456\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Here&#8217;s a simple example to use the\u00a0<strong>appendTo() method<\/strong>. This example assumes that we have 2 main divs with class &#8220;red&#8221; &amp; &#8220;green&#8221; applied to them &amp; these divs are held in another &#8220;container&#8221; div for easy manipulation. So what we are going to do is have 2 individual buttons that will let you appendTo the red div to green div and vice-versa. We also have another button that will reset the divs back to their original state, once the jquery appendTo method has been used on them dynamically.<\/p>\n<h3>Example for .appendTo() method:<\/h3>\n<p>Here is the complete source code:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;jjQuer .appendTo() method Examples&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery-1.8.2.min.js\"&gt;&lt;\/script&gt;\n&lt;style type=\"text\/css\"&gt;\n.red {\nbackground-color: #FFA6A6;\nborder: 1px solid red;\nmargin: 10px;\n}\n.green {\nbackground-color: #CEFFCE;\nborder: 1px solid green;\nmargin: 10px;\n}\n&lt;\/style&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;div class=\"divs_container\"&gt;\n\n&lt;div id=\"mydiv1\" class=\"red\"&gt;This is my 1 div&lt;\/div&gt;\n\n&lt;div id=\"mydiv2\" class=\"green\"&gt;This is my 2 div&lt;\/div&gt;\n\n&lt;\/div&gt;\n\n&lt;input type=\"button\" name=\"sbt_append_g2r\" id=\"sbt_append_g2r\" value=\"Append Green Div to Red\"&gt;\n&lt;input type=\"submit\" name=\"sbt_remove\" id=\"sbt_remove\" value=\"Unappend\/Remove\"&gt;\n&lt;input type=\"button\" name=\"sbt_append_r2g\" id=\"sbt_append_r2g\" value=\"Append Red Div to Green\"&gt;\n\n&lt;script type=\"text\/javascript\" language=\"javascript\" &gt;\n\n$(document).ready(function(){\n\nvar divs_container_html = $(\".divs_container\").html();\n\n\/\/Append Green Div to Red\n$('#sbt_append_g2r').on(\"click\", function(){\n\n$('.green').appendTo($('.red'));\n\n});\n\n\/\/Append Red Div to Green\n$('#sbt_append_r2g').on(\"click\", function(){\n\n$('.red').appendTo($('.green'));\n\n});\n\n\/\/Reset the divs back to their original state\n$('#sbt_remove').on(\"click\", function(){\n\n$(\".divs_container\").html(divs_container_html);\n\n});\n\n});\n\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1456\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h2>Difference between jquery .append() and jquery .appendTo()<\/h2>\n<p>In jQuery, the .append() and .appendTo() methods have the same functioanlity. However, <strong>the main difference lies in the syntax<\/strong>. To be more precise, the syntax varies, in the placement of the content and target. Here&#8217;s an easy represenatation for your understanding:<\/p>\n<h3>Using .append();<\/h3>\n<pre class=\"lang:js decode:true\">$(target).append(new_content);<\/pre>\n<h3>Using .appendTo();<\/h3>\n<pre class=\"lang:js decode:true\">$(new_content).appendTo(target);<\/pre>\n<p>Where the new_content can be a div, an image, HTML content, etc. If you would like a technical explanation of the above, here it is:<\/p>\n<p>When .append() is used, the container into which the content is inserted appears <strong>before<\/strong> the method.<\/p>\n<p>When .appendTo() is used,\u00a0the container into which the content is inserted appears <strong>after\u00a0<\/strong>the method.<\/p>\n<p><strong>That&#8217;s it for now!<\/strong><\/p>\n<p>Do you know of any other ways to use <strong>.appendTo() method using jQuery<\/strong>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using jQuery appendTo method, you can easily add content, dynamic or static. This is immensely helpful with inserting dynamic content in a div or any other element. In this article, I am going to share info on the appendTo() method along with easy to follow examples.<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[40,42,41],"class_list":["post-1456","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-2","tag-jquery-demos-2","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-nu","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=1456"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1456\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=1456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=1456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=1456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}