Home » JavaScript – document.getElementsByTagName() method

JavaScript – document.getElementsByTagName() method

by Online Tutorials Library

Javascript – document.getElementsByTagName() method

The document.getElementsByTagName() method returns all the element of specified tag name.

The syntax of the getElementsByTagName() method is given below:

Here, name is required.

Example of document.getElementsByTagName() method

In this example, we going to count total number of paragraphs used in the document. To do this, we have called the document.getElementsByTagName(“p”) method that returns the total paragraphs.


Output of the above example

This is a pragraph

Here we are going to count total number of paragraphs by getElementByTagName() method.

Let’s see the simple example

Another example of document.getElementsByTagName() method

In this example, we going to count total number of h2 and h3 tags used in the document.


Output of the above example

This is h2 tag

This is h2 tag

This is h3 tag

This is h3 tag

This is h3 tag

Note: Output of the given examples may differ on this page because it will count the total number of para , total number of h2 and total number of h3 tags used in this document.

You may also like