Counting elements with class

This script will give you the number of elements containing a specific class. used on: http://raccoon.ninja

by Davi Amorim

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="result"></div>

<h1 class="foo">I'm a H1!</h1>
<h2 class="foo">I'm a H2!</h2>
<p class="foo">I'm a P!</p>
<span class="foo">I'm a SPAN!</span>

<h1 class="bar">I'm another H1!</h1>
<p class="bar">I'm another P!</p>
<span class="bar">I'm another SPAN!</span>

CSS

#result {
  margin-top: 20px;
  padding: 10px;
  background: #64FF33;
}

JavaScript

let className = "foo";
let qty_items = $("."+className).length;

$("div#result").text("Number of elements with class " + className + ": "+ qty_items);