{"id":2577,"date":"2013-03-02T13:11:14","date_gmt":"2013-03-02T13:11:14","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2577"},"modified":"2013-03-02T13:11:14","modified_gmt":"2013-03-02T13:11:14","slug":"using-jquery-add-class-to-div-easy-examples-to-add-class-to-div","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/using-jquery-add-class-to-div-easy-examples-to-add-class-to-div\/","title":{"rendered":"jQuery Add Class To Div Examples"},"content":{"rendered":"<p>In previous articles, we seen how to use jQuery to add div, add an element to div, add div to div, add text to div &amp; add html to div. Now let&#8217;s say that you have a requirement for changing a div style on the fly i.e. you would like to change the way a div appears using a class. In this article, I am going to share easy examples of using<strong> <\/strong><em><strong>jQuery to add class to div.<\/strong><\/em><\/p>\n<p><!--more--><\/p>\n<h2>How to use jQuery to Add Class 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=2577\" 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>Let&#8217;s assume that we have a div with ID &#8220;my_div&#8221;. You would now like to add class to this div. There are 3 cases in this situation.<\/p>\n<p>1. Add class to a div that does not have any another classes.<\/p>\n<p>2. Add class to a div and replace any\/all classed that it may have.<\/p>\n<p>3. Add class to a div and simply append\/add the new class to existing class(es).<\/p>\n<p>So let&#8217;s take a look at each of these cases by means of examples.<\/p>\n<h3>Example 1: Add class to a div that does not have any another classes<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 1\">&lt;style type=\"text\/css\"&gt;\n.existing_yellow {\n\tbackground-color: #FFFF99;\n\tborder: 1px solid #FF0;\n}\n.existing_purple {\n\tbackground-color: #9999FF;\n\tborder: 1px solid #90F;\n}\n.green {\n\tbackground-color: #D7FFD7;\n\tborder: 1px solid #090;\n}\n.blue {\n\tbackground-color: #E1F3FF;\n\tborder: 1px solid #09F;\n}\n.red_text {\n\tcolor: #D70000;\n\tfont-weight: bold;\n}\n.orange {\t\n\tborder: 10px solid #F90;\n\tcolor: #FFF;\n\tfont-size: 200%;\n\tfont-weight:bold;\n\tfont-family: Georgia, \"Times New Roman\", Times, serif;\n\tfont-style: italic;\t\n}\n\n&lt;\/style&gt;\n\n&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 Green class to DIV 1\"&gt;\n\n\t\/\/This will add class to the a div that does NOT have any class applied to it already\n\t$('#sbt_add2div1').on(\"click\", function(e){\t\t\n\n\t\t$('#my_div1').addClass('green');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 2: Add class to a div and replace any\/all classed that it may have<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 2\">&lt;style type=\"text\/css\"&gt;\n.existing_yellow {\n\tbackground-color: #FFFF99;\n\tborder: 1px solid #FF0;\n}\n.existing_purple {\n\tbackground-color: #9999FF;\n\tborder: 1px solid #90F;\n}\n.green {\n\tbackground-color: #D7FFD7;\n\tborder: 1px solid #090;\n}\n.blue {\n\tbackground-color: #E1F3FF;\n\tborder: 1px solid #09F;\n}\n.red_text {\n\tcolor: #D70000;\n\tfont-weight: bold;\n}\n.orange {\t\n\tborder: 10px solid #F90;\n\tcolor: #FFF;\n\tfont-size: 200%;\n\tfont-weight:bold;\n\tfont-family: Georgia, \"Times New Roman\", Times, serif;\n\tfont-style: italic;\t\n}\n&lt;\/style&gt;\n\n&lt;div id=\"my_div2\" class=\"existing_yellow red_text\"&gt;This is MY DIV 2&lt;\/div&gt;\n\n&lt;input type=\"submit\" name=\"sbt_add2div2\" id=\"sbt_add2div2\" value=\"Replace all classes of DIV 2 with Blue Class\"&gt;\n\n\t\/\/This will add class to the a div that HAS classes applied to it already and will replace all existing classes with new class\n\t$('#sbt_add2div2').on(\"click\", function(e){\t\t\n\n\t\t$('#my_div2').attr('class', 'blue');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 3: Add class to a div and simply append\/add the new class to existing class(es)<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 3\">&lt;style type=\"text\/css\"&gt;\n.existing_yellow {\n\tbackground-color: #FFFF99;\n\tborder: 1px solid #FF0;\n}\n.existing_purple {\n\tbackground-color: #9999FF;\n\tborder: 1px solid #90F;\n}\n.green {\n\tbackground-color: #D7FFD7;\n\tborder: 1px solid #090;\n}\n.blue {\n\tbackground-color: #E1F3FF;\n\tborder: 1px solid #09F;\n}\n.red_text {\n\tcolor: #D70000;\n\tfont-weight: bold;\n}\n.orange {\t\n\tborder: 10px solid #F90;\n\tcolor: #FFF;\n\tfont-size: 200%;\n\tfont-weight:bold;\n\tfont-family: Georgia, \"Times New Roman\", Times, serif;\n\tfont-style: italic;\t\n}\n&lt;\/style&gt;\n\n&lt;div id=\"my_div3\" class=\"existing_purple\"&gt;This is MY DIV 3&lt;\/div&gt;    \n\n&lt;input type=\"submit\" name=\"sbt_add2div3\" id=\"sbt_add2div3\" value=\"Add Orange Class to existing class of DIV 3\"&gt;\n\n\t\/\/This will retain all existing class and simply add\/append your new class to existing classes\n\t$('#sbt_add2div3').on(\"click\", function(e){\t\t\t\t\n\n\t\t$('#my_div3').addClass('orange');\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=2577\" 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<h4>Your Turn!<\/h4>\n<p>Do you know of any other ways to use \u00a0<strong><em>jQuery to add class to div<\/em><\/strong>? Feel free to share by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In previous articles, we seen how to use jQuery to add div, add an element to div, add div to div, add text to div &amp; add html to div. Now let&#8217;s say that you have a requirement for changing a div style on the fly i.e. you would like to change the way a [&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-2577","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-Fz","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2577","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=2577"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2577\/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=2577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}