{"id":1391,"date":"2012-11-01T18:24:13","date_gmt":"2012-11-01T18:24:13","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=1391"},"modified":"2012-11-01T18:24:13","modified_gmt":"2012-11-01T18:24:13","slug":"jquery-count-elements-count-elements-with-class-using-jquery","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-count-elements-count-elements-with-class-using-jquery\/","title":{"rendered":"jQuery Count Elements Examples"},"content":{"rendered":"<p>When working with different elements of a web page, it might be necessary at times to count the number of elements by using their class. In this article, I am going to share a very easy way to <em><strong>count the number of elements that have the same class using jQuery<\/strong><\/em>. The actual code is just 1 line, so it&#8217;s super easy to follow along.<!--more--><\/p>\n<h2>How to Count Elements with Class using jQuery &#8211; Examples<\/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=1391\" 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 got few divs in our web page and they all have a class of &#8220;my_class&#8221; applied to them. In such a case, we can count the number of divs easily. Following is the complete code (HTML + jQuery):<\/p>\n<h3>Example: Count Elements with Class using jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true crayon-selected\" title=\"Example\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Example&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.my_class {\n\tbackground-color: #DBF0FF;\n\tborder: 1px solid #06F;\n}\n.my_class2 {\n\tbackground-color: #FFDF9D;\n\tborder: 1px solid #F90;\n}\n.my_class3 {\n\tbackground-color: #D7FFD7;\n\tborder: 1px solid #090;\n}\n\n&lt;\/style&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;div class=\"my_class\"&gt;This is my 1 div&lt;\/div&gt;\n\n&lt;div class=\"my_class\"&gt;This is my 2 div&lt;\/div&gt;\n\n&lt;div class=\"my_class\"&gt;This is my 3 div&lt;\/div&gt;\n\n&lt;div class=\"my_class2\"&gt;This is my 4 div&lt;\/div&gt;\n\n&lt;div class=\"my_class2\"&gt;This is my 5 div&lt;\/div&gt;\n\n&lt;div class=\"my_class3\"&gt;This is my 6 div&lt;\/div&gt;\n\n&lt;p&gt;\n  &lt;input type=\"submit\" name=\"sbt_count\" id=\"sbt_count\" value=\"Count all divs that have .my_class (blue color background) applied to them \"&gt;\n&lt;\/p&gt;\n\n&lt;script type=\"text\/javascript\" language=\"javascript\" &gt;\n\n$(document).ready(function(){\n\n\t$('#sbt_count').on(\"click\", function(e){\t\t\n\n\t\talert( $('.my_class').length );\n\n\t\te.preventDefault();\n\n\t});\t\t\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=1391\" 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><strong>Simple, isn&#8217;t it?<\/strong><\/p>\n<p>Do you know of any other ways to <em><strong>get the count of elements with specific class using jQuery<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with different elements of a web page, it might be necessary at times to count the number of elements by using their class. In this article, I am going to share a very easy way to count the number of elements that have the same class using jQuery. The actual code is just [&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":[40,42,41],"class_list":["post-1391","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-mr","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1391","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=1391"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1391\/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=1391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=1391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=1391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}