Last updated: Mar 5, 2024
Reading timeยท5 min
Use the document.querySelector()
method to get an element by id by partially
matching a string.
The method returns the first element within the document that matches the provided selector.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div id="box1">Box 1</div> <script src="index.js"></script> </body> </html>
Use a caret ^
symbol to get the first DOM element that has an id
attribute
whose value starts with a specific string.
// โ get an element by ID that STARTS WITH `bo` const element1 = document.querySelector(`[id^="bo"]`); console.log(element1); // ๐๏ธ div#box1
The code sample selects an element that has an id
attribute whose value starts
with bo
.
We used the
document.querySelector method to
get an element by partially matching its id
attribute.
If no matches are found, the method returns null
.
If you need to select all elements that have an id
starting with a specific
value, use the querySelectorAll
method instead.
const elements = document.querySelectorAll(`[id^="bo"]`); console.log(elements); // ๐๏ธ [div#box1]
^
symbol, which has the same meaning when used with regular expressions.Use a dollar $
sign to select the first DOM element that has an id
attribute
with a value that ends with a specific string.
// โ get an element by ID that ENDS WITH `ox1` const element2 = document.querySelector(`[id$="ox1"]`); console.log(element2); // ๐๏ธ div#box1
If you need to select all DOM elements that have an id
that ends with a
specific string, use document.querySelectorAll()
instead.
const elements = document.querySelectorAll(`[id$="ox1"]`); console.log(elements); // ๐๏ธ [div#box1]
The dollar sign $
signifies the end of the input in regular expressions and
has the same meaning in selectors.
The code sample selects the first DOM element that has an id
attribute whose
value ends with ox1
.
Use an asterisk *
to get the first DOM element that has an id
attribute
whose value contains a specific string.
// โ get an element by ID that CONTAINS `ox` const element3 = document.querySelector(`[id*="ox"]`); console.log(element3); // ๐๏ธ div#box1
If you need to select all DOM elements that have an id
that ends with a
specific string, use document.querySelectorAll()
instead.
const elements = document.querySelectorAll(`[id*="ox"]`); console.log(elements); // ๐๏ธ [div#box1]
The string ox
could be located anywhere in the value of the id
attribute for
the condition to be met.
You could also prefix the selector with the specific type of element you want to match to narrow down the results.
// โ get a DIV element by ID that STARTS WITH `bo` const element1 = document.querySelector(`div[id^="bo"]`); console.log(element1); // ๐๏ธ div#box1
The example above selects a div
element that has an id
attribute with a
value that starts with bo
.
If no element matches the provided selector, the querySelector
method returns
null
.
To make sure you don't try to access a property on a null
value or try to
invoke a method on one, you should handle this scenario.
const element1 = document.querySelector(`div[id^="bo"]`); console.log(element1); // ๐๏ธ div#box1 // ๐๏ธ Check if element1 stores truthy value if (element1) { // ๐๏ธ "Box 1" console.log(element1.textContent); }
We checked if the element1
variable stores a truthy value before accessing the
textContent
property.
This helps us make sure we don't get an error by trying to access the property
on a null
value.
Use the document.querySelectorAll()
method to get all elements whose id starts
with a specific string.
The method returns a NodeList
containing all the elements that match the
provided selector.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div id="box1">Box 1</div> <div id="box2">Box 2</div> <div id="box3">Box 3</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// โ get ALL elements whose ID starts with `box` const elements1 = document.querySelectorAll(`[id^="box"]`); console.log(elements1); // ๐๏ธ [div#box1, div#box2, div#box3] // โ get FIRST element whose ID starts with `box` const element1 = document.querySelector(`[id^="box"]`); console.log(element1); // ๐๏ธ [div#box1]
id
that starts with a specific string, use the querySelector
method instead.We used the document.querySelectorAll
method to get a collection of the elements that have an id
attribute whose
value starts with box
.
If no matches are found, the method returns an empty NodeList
.
The example selects all the elements in the document that have an id
attribute
with a value starting with box
.
const elements1 = document.querySelectorAll(`[id^="box"]`); console.log(elements1); // ๐๏ธ [div#box1, div#box2, div#box3]
^
symbol, which has the same meaning when used in regular expressions.id
starting with a specific stringYou could also prefix the selector with the specific type of element you want to match, to narrow down the results.
// โ get all DIV elements whose ID starts with `box` const elements1 = document.querySelectorAll(`div[id^="box"]`); console.log(elements1); // ๐๏ธ [div#box1, div#box2, div#box3]
The example selects all div
elements that have an id
attribute, whose value
starts with box
.
If you need to iterate over the collection of elements, use the forEach()
method.
const elements1 = document.querySelectorAll(`div[id^="box"]`); elements1.forEach(element => { console.log(element.textContent); });
The function we passed to the forEach
method gets called with each element in
the NodeList
.
You could pass multiple selectors to the querySelectorAll
method if you need
to get elements with id
starting with multiple different values.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div id="box1">Box 1</div> <div id="box2">Box 2</div> <div id="container1">Container 1</div> <div id="container2">Container 2</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// โ get all by ID starting with `box` or `container` const elements1 = document.querySelectorAll(`[id^="box"], [id^="container"]`); // ๐๏ธ [div#box1, div#box2, div#container1, div#container2] console.log(elements1);
We selected all the DOM elements which have an id
attribute, whose value
starts with box
or container
.
You can pass as many valid CSS selectors as necessary to the querySelectorAll
method, just make sure they are separated by commas.
You can learn more about the related topics by checking out the following tutorials: