{"id":1965,"date":"2013-01-13T01:08:29","date_gmt":"2013-01-13T01:08:29","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=1965"},"modified":"2013-01-13T01:08:29","modified_gmt":"2013-01-13T01:08:29","slug":"jquery-class-jquery-class-selector-select-element-with-single-multiple-class","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-class-jquery-class-selector-select-element-with-single-multiple-class\/","title":{"rendered":"jQuery Class Examples &#8211; Select Element With Single \/ Multiple Class"},"content":{"rendered":"<p>If you are wanting to know how use\u00a0<em><strong>jQuery to target Class attribute\u00a0<\/strong><\/em> of an element &amp; if want to be able to select element with Single \/ Multiple Class, then this article is for you. In this article, I will show you how to select an element with just a single class &amp; also select an element which has multiple classes applied to it. Read on to find out more.<!--more--><\/p>\n<h2>How to use access the Class attribute of an element using jQuery<\/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=1965\" 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>For the sake of the example, we are going to assume that we have few divs &amp; each of these divs have at least one or more class applied to them.<\/p>\n<p><strong>Example: Complete HTML + jQuery source code<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\" title=\"Example\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Select Element With Single \/ Multiple Class in jQuery&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery-1.8.2.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;div id=\"my_div1\" class=\"my_class1\"&gt;This is some content.&lt;\/div&gt;\n\n&lt;div id=\"my_div2\" class=\"my_class1 my_class2\"&gt;This is some content.&lt;\/div&gt;\n\n&lt;div id=\"my_div3\" class=\"my_class3\"&gt;This is some content.&lt;\/div&gt;\n\n&lt;div id=\"my_div4\" class=\"my_class4 my_class3\"&gt;This is some content.&lt;\/div&gt;\n\n&lt;div id=\"my_div5\" class=\"my_class5 my_class1 my_class2 my_class3 myclass4\"&gt;This is some content.&lt;\/div&gt;\n\n&lt;br&gt;\n\n&lt;input type=\"submit\" name=\"get1\" id=\"get1\" value=\"Get elements with my_class1\" \/&gt;&lt;br&gt;\n\n&lt;input type=\"submit\" name=\"get2\" id=\"get2\" value=\"Get elements with my_class1 &amp;amp; my_class2\" \/&gt;&lt;br&gt;\n\n&lt;input type=\"submit\" name=\"get3\" id=\"get3\" value=\"Get elements with my_class3\" \/&gt;&lt;br&gt;\n\n&lt;input type=\"submit\" name=\"get4\" id=\"get4\" value=\"Get elements with my_class3 &amp;amp; my_class4\" \/&gt;&lt;br&gt;\n\n&lt;input type=\"submit\" name=\"get5\" id=\"get5\" value=\"Get elements with my_class5\" \/&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\n$('#get1').click(function() {\n\n\talert( $(\".my_class1\").attr('id') );\n\n});\n\n$('#get2').click(function() {\n\n\t$(\".my_class1.my_class2\").each( function() {\n\t\t\talert( $(this).attr(\"id\") );\n\t})\n\n});\n\n$('#get3').click(function() {\n\n\t$(\".my_class3\").each( function() {\n\t\t\talert( $(this).attr(\"id\") );\n\t})\n\n});\n\n$('#get4').click(function() {\n\n\t$(\".my_class3.my_class4\").each( function() {\n\t\t\talert( $(this).attr(\"id\") );\n\t})\n\n});\n\n$('#get5').click(function() {\n\n\t$(\".my_class5\").each( function() {\n\t\t\talert( $(this).attr(\"id\") );\n\t})\n\n});\n\n&lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<h2>Select Element With Single \/ Multiple Class<\/h2>\n<p>In order to be able to show all elements with the corresponding class applied to it, it&#8217;s essential that we do it in a loop. Hence this looping is done by using the iterator function &#8220;each&#8221;. If &#8220;each&#8221; is not used, then only the first div that has the class applied will be shown &amp; the following div will be ignored, even if it has the class applied to it.<\/p>\n<p>So that&#8217;s how you use jQuery Class Selector to select element with <strong>single or multiple classes<\/strong> applied to it.<\/p>\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=1965\" 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>Simple, isn&#8217;t it?<\/h4>\n<p>Do you know of any other ways to use <em><strong>access\u00a0<\/strong><strong>class of an element using jQuery<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are wanting to know how use\u00a0jQuery to target Class attribute\u00a0 of an element &amp; if want to be able to select element with Single \/ Multiple Class, then this article is for you. In this article, I will show you how to select an element with just a single class &amp; also select [&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":[86,42,41],"class_list":["post-1965","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-class","tag-jquery-demos-2","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-vH","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1965","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=1965"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1965\/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=1965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=1965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=1965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}