{"id":2587,"date":"2013-03-05T14:38:36","date_gmt":"2013-03-05T14:38:36","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2587"},"modified":"2013-03-05T14:38:36","modified_gmt":"2013-03-05T14:38:36","slug":"using-jquery-add-content-to-div-easy-examples-to-add-content-to-div","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/using-jquery-add-content-to-div-easy-examples-to-add-content-to-div\/","title":{"rendered":"Easy jQuery Add Content to Div Examples"},"content":{"rendered":"<p>jQuery allows lots of div manipulation. There is virtually nothing that you can think of, that you can&#8217;t do using jQuery. So\u00a0are you familiar with adding content to a div? If not, then read this article to the last as I am going to share with you easy ways to <strong>add content to div using jQuery<\/strong>. I will be showing examples wherever applicable, so it should be super easy to follow along.<\/p>\n<p><!--more--><\/p>\n<h2>How to use jQuery to add content to div<\/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=2587\" 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>There are 2 possibilities and there are 2 ways to handle each of these 2 possibilities. So all in all, we will have 4 variations of adding content to div using jQuery. They are the following:<\/p>\n<ol>\n<li>Add textual content\/ simple string content before the content of the div begins.<\/li>\n<li>Add textual content\/ simple string content after the content of the div ends.<\/li>\n<li>Add HTML content\/ simple string content before the content of the div begins.<\/li>\n<li>Add HTML content\/ simple string content after the content of the div ends.<\/li>\n<\/ol>\n<p>In all the above cases, the content will become a part of the div i.e. the content will be inside of the div. That said, let&#8217;s see how we can add varying custom content to a div practically by means of examples.<\/p>\n<h3>Here are the common styles used for all the examples. Use them for testing purposes:<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Common CSS styles for all examples used below\">&lt;style type=\"text\/css\"&gt;\n#my_div1 {\n\tbackground-color: #D7FFD7;\n\tborder: 1px solid #090;\n}\n#my_div2 {\n\tbackground-color: #E1F3FF;\n\tborder: 1px solid #09F;\n}\n#my_div3 {\n\tbackground-color: #FC9;\n\tborder: 1px solid #F90;\n}\n\n#my_div4 {\n\tbackground-color: #9999FF;\n\tborder: 1px solid #90F;\n}\n&lt;\/style&gt;<\/pre>\n<h3>Example 1: Add textual content\/ simple string content before the content of the div begins<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 1: Adding textual content\/ simple string content before the content of the div begins\">&lt;div id=\"my_div1\"&gt;This is MY DIV 1&lt;\/div&gt;\n\n&lt;input type=\"submit\" name=\"sbt_add2div1\" id=\"sbt_add2div1\" value=\"Add Simple text BEFORE content of DIV 1\"&gt;\n\n\t\/\/This will add simple textual string BEFORE the content of the div, inside the div\n\t$('#sbt_add2div1').on(\"click\", function(e){\t\t\n\n\t\t$('#my_div1').prepend('This is Simple Text.');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 2: Add textual content\/ simple string content after the content of the div ends<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 2: Adding textual content\/ simple string content after the content of the div ends\">&lt;div id=\"my_div2\"&gt;This is MY DIV 2&lt;\/div&gt;\n\n&lt;input type=\"submit\" name=\"sbt_add2div2\" id=\"sbt_add2div2\" value=\"Add Simple text AFTER content of DIV 2\"&gt;\n\n\t\/\/This will add simple textual string AFTER the content of the div, inside the div\n\t$('#sbt_add2div2').on(\"click\", function(e){\t\t\n\n\t\t$('#my_div2').append('This is Simple Text.');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 3: Add HTML content\/ simple string content before the content of the div begins<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 3 : Adding HTML content\/ simple string content before the content of the div begins\">&lt;div id=\"my_div3\"&gt;This is MY DIV 3&lt;\/div&gt;  \n\n&lt;input type=\"submit\" name=\"sbt_add2div3\" id=\"sbt_add2div3\" value=\"Add HTML content BEFORE content of DIV 3\"&gt;\n\n\t\/\/This will add HTML content BEFORE the content of the div, inside the div\n\t$('#sbt_add2div3').on(\"click\", function(e){\t\t\t\t\n\n\t\t$('#my_div3').prepend('&lt;p&gt;This is HTML code for a PARAGRAPH.&lt;\/p&gt;');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 4: Add HTML content\/ simple string content after the content of the div ends<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 4: Adding HTML content\/ simple string content after the content of the div ends\">&lt;div id=\"my_div4\"&gt;This is MY DIV 4&lt;\/div&gt;        \n\n&lt;input type=\"submit\" name=\"sbt_add2div4\" id=\"sbt_add2div4\" value=\"Add HTML content AFTER content of DIV 4\"&gt;\n\n\t\/\/This will add HTML content AFTER the content of the div, inside the div\n\t$('#sbt_add2div4').on(\"click\", function(e){\t\t\t\t\n\n\t\t$('#my_div4').append('&lt;p&gt;This is HTML code for a PARAGRAPH.&lt;\/p&gt;');\n\n\t\te.preventDefault();\n\n\t});<\/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=2587\" 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<h3>Conclusion:<\/h3>\n<p>1. So regardless of whether you want to add simple textual content to content of div or want to add html content to div content, the\u00a0procedure\u00a0is the same.<\/p>\n<p>2. Use .prepend() if you want to add content before the content of the div.<\/p>\n<p>3. Use .append() if you want to\u00a0add content after the content of the div.<\/p>\n<h4>Your Turn!<\/h4>\n<p>Do you know of any other ways to use <em><strong>jQuery to\u00a0add content to div<\/strong><\/em>? Feel free to share by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery allows lots of div manipulation. There is virtually nothing that you can think of, that you can&#8217;t do using jQuery. So\u00a0are you familiar with adding content to a div? If not, then read this article to the last as I am going to share with you easy ways to add content to div using [&hellip;]<\/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":[42,43,41],"class_list":["post-2587","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-demos-2","tag-jquery-snippets","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-FJ","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2587","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=2587"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2587\/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=2587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}