Get all Elements by Type using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
3 min

banner

# Get all Elements by Type using JavaScript

Use the querySelectorAll() method to get all elements by type.

The method returns a NodeList containing the elements that match the provided selector.

Here is the HTML for the examples.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <input type="text" id="first_name" /> <input type="number" id="age" /> <script src="index.js"></script> </body> </html>
The code for this article is available on GitHub

And here is the related JavaScript code.

index.js
// βœ… Get all input elements with type = "text" const elements1 = document.querySelectorAll( 'input[type="text"]', ); console.log(elements1); // πŸ‘‰οΈ [input#first_name] // ------------------------------------------------------- // βœ… Get all input elements const elements2 = document.querySelectorAll('input'); console.log(elements2); // πŸ‘‰οΈ [input#first_name, input#age] // ------------------------------------------------------- // βœ… Get all input elements const elements3 = document.getElementsByTagName('input'); console.log(elements3); // πŸ‘‰οΈ [input#first_name, input#age]

get all elements by type

The code for this article is available on GitHub

# Get all elements by type attribute

In the first example, we used the document.querySelectorAll() method to get all input elements that have a type attribute with a value of text.

index.js
const elements1 = document.querySelectorAll('input[type="text"]'); console.log(elements1); // πŸ‘‰οΈ [input#first_name] console.log(elements1[0]); // πŸ‘‰οΈ input#first_name

get all elements by type attribute

The code for this article is available on GitHub

If you want to get a specific element, you can access the NodeList at an index.

JavaScript indexes are zero-based, so the first element in the NodeList has an index of 0.

# Select all elements that have a type attribute set to text

You can adjust the selector as needed, e.g. the following example selects any type of element with a type attribute set to a value of text (not just input elements).

index.js
const elements = document.querySelectorAll('[type="text"]'); console.log(elements); // πŸ‘‰οΈ NodeListΒ [input#first_name]

select all elements that have type attribute set to text

The code for this article is available on GitHub

# Get all input elements on the page

In the second example, we used the querySelectorAll method to get a NodeList of all of the input tags.

index.js
const elements = document.querySelectorAll('input'); console.log(elements); // πŸ‘‰οΈ NodeList(2)Β [input#first_name, input#age]

# Iterating over a collection of elements

If you need to iterate over the collection, use the forEach() method.

index.js
const elements = document.querySelectorAll('input'); console.log(elements); // πŸ‘‰οΈ [input#first_name, input#age] elements.forEach(element => { console.log(element); });
The code for this article is available on GitHub

# Get all elements by Type using getElementsByTagName

The third example uses the document.getElementsByTagName() method to select all input tags.

index.js
const elements = document.getElementsByTagName('input'); console.log(elements); // πŸ‘‰οΈ [input#first_name, input#age]
This serves as an alternative to the querySelectorAll method and you might see the getElementsByTagName method used in older code bases.

# Iterating over an HTMLCollection

The method returns an HTMLCollection, which you have to convert to an array if you need to use methods like forEach().

index.js
const elements = Array.from(document.getElementsByTagName('input')); console.log(elements); // πŸ‘‰οΈ [input#first_name, input#age] elements.forEach(element => { console.log(element); });
The code for this article is available on GitHub

By converting the HTMLCollection to an array, we are able to use any of the array-supported methods.

You can also use a for...of loop to iterate over the collection.

index.js
const elements = Array.from(document.getElementsByTagName('input')); console.log(elements); // πŸ‘‰οΈ [input#first_name, input#age] for (const element of elements) { console.log(element); }

If you need to get a specific element, access the array at an index.

index.js
const elements = Array.from(document.getElementsByTagName('input')); console.log(elements); // πŸ‘‰οΈ [input#first_name, input#age] console.log(elements[0]); // inptu#first-name console.log(elements[elements.length - 1]); // input#age
The code for this article is available on GitHub

The first element in the array has an index of 0 and the last element has an index of array.length - 1.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.