ElkunCoding

To count the number of elements with a specific class in JavaScript, you can use the querySelectorAll method to select all elements with that class, and then use the length property to get the number of elements.

Here’s an example:

// Select all elements with the class "my-class" 

var elements = document.querySelectorAll('.my-class'); 
// Get the number of elements 
var count = elements.length; 
console.log(count); 
// Outputs the number of elements with the class "my-class"

You can also use the getElementsByClassName method to achieve the same result:

// Select all elements with the class "my-class" 
var elements = document.getElementsByClassName('my-class'); 
// Get the number of elements 
var count = elements.length; 
console.log(count); 
// Outputs the number of elements with the class "my-class"

Note that getElementsByClassName returns a live HTMLCollection, which means that it will update automatically as elements with the specified class are added or removed from the document.