Skip to content
the poor coder

js count element tags

Poor Man
Published
Reading time
2 min read
On this page

JS Count Element Tags#

Counting element tags in HTML documents is an essential task for web developers. It helps to keep track of the number of times a particular tag appears in a web page. In this article, we will discuss multiple ways to count HTML element tags using JavaScript.

Method 1: Using getElementsByTagName() method#

The getElementsByTagName() method returns a collection of all elements in the document with the specified tag name. We can use this method to count the number of times a specific tag appears in the HTML document.

JavaScript
const numberOfElements = document.getElementsByTagName('div').length;
console.log(`Number of <div> tags: ${numberOfElements}`);

In the above code, we are using the getElementsByTagName() method to get all the <div> tags in the document and then counting their length.

Method 2: Using querySelectorAll() method#

We can also use the querySelectorAll() method to select all elements that match a specific CSS selector. This method returns a NodeList object that contains all matching elements.

JavaScript
const numberOfElements = document.querySelectorAll('div').length;
console.log(`Number of <div> tags: ${numberOfElements}`);

In the above code, we are using the querySelectorAll() method to select all <div> tags in the document and then counting their length.

Method 3: Using reduce() method#

We can also use the reduce() method to count the number of times a specific tag appears in the HTML document. This method applies a function against an accumulator and each element in the array to reduce it to a single value.

JavaScript
const elements = Array.from(document.getElementsByTagName('*'));
const numberOfElements = elements.reduce((accumulator, element) => {
  if (element.tagName.toLowerCase() === 'div') {
    return accumulator + 1;
  }
  return accumulator;
}, 0);

console.log(`Number of <div> tags: ${numberOfElements}`);

In the above code, we are using the reduce() method to count the number of <div> tags in the document. We are first converting the NodeList object returned by getElementsByTagName('*') to an array using Array.from() method. Then, we are applying the reduce() method on the array and checking if the tag name is equal to 'div'. If it is, we are incrementing the accumulator value by 1.

Conclusion#

In this article, we discussed three different methods to count HTML element tags using JavaScript. We can use any of these methods depending on our requirements.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC