Last updated: Mar 5, 2024
Reading timeΒ·3 min
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.
<!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>
And here is the related JavaScript code.
// β 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]
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
.
const elements1 = document.querySelectorAll('input[type="text"]'); console.log(elements1); // ποΈ [input#first_name] console.log(elements1[0]); // ποΈ input#first_name
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
.
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).
const elements = document.querySelectorAll('[type="text"]'); console.log(elements); // ποΈ NodeListΒ [input#first_name]
In the second example, we used the querySelectorAll
method to get a NodeList
of all of the input
tags.
const elements = document.querySelectorAll('input'); console.log(elements); // ποΈ NodeList(2)Β [input#first_name, input#age]
If you need to iterate over the collection, use the forEach()
method.
const elements = document.querySelectorAll('input'); console.log(elements); // ποΈ [input#first_name, input#age] elements.forEach(element => { console.log(element); });
The third example uses the
document.getElementsByTagName()
method to select all input
tags.
const elements = document.getElementsByTagName('input'); console.log(elements); // ποΈ [input#first_name, input#age]
querySelectorAll
method and you might see the getElementsByTagName
method used in older code bases.The method returns an HTMLCollection
, which you have to convert to an array if
you need to use methods like forEach()
.
const elements = Array.from(document.getElementsByTagName('input')); console.log(elements); // ποΈ [input#first_name, input#age] elements.forEach(element => { console.log(element); });
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.
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.
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 first element in the array has an index of 0
and the last element has an
index of array.length - 1
.
You can learn more about the related topics by checking out the following tutorials: