How to Count Elements with a Specific Class using Vanilla JS
Count your elements containing a certain class with pure vanilla JS. Simple and Fast Snippet to include in your projects.
Here is a small snippet to get you started
// ✅ Count all elements with class of blue const countAll = document.querySelectorAll('.blue').length; console.log(countAll); // 👉️ 3 // ✅ Count only elements in the div with `id` = `box`, // that have a class of blue const countInDiv = document.querySelectorAll('#box .blue').length; console.log(countInDiv); // 👉️ 2