Last updated: Mar 4, 2024
Reading timeยท7 min
Use the querySelector()
method to get a DOM element by attribute.
The querySelector
method will return the first element in the document that
matches the specified attribute.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div title="first" class="box">Box 1</div> <div title="second" class="box">Box 2</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// โ querySelector for single element const el = document.querySelector('[title="first"]'); console.log(el); // ๐๏ธ div // โ querySelectorAll for all elements matching attribute const elements = document.querySelectorAll('[class="box"'); console.log(elements); // ๐๏ธ [div.box, div.box]
The first example uses the
document.querySelector method to
get the first element in the document that has a title
attribute set to
first
.
If you need a collection of elements, use the
document.querySelectorAll method and if
you need the first element that matches the provided selector, use the
document.querySelector
method.
The second example selects all the elements in the document that have a class
attribute set to box
.
You can also narrow things down by selecting only div
elements that have a
specific attribute or selecting elements that have the attribute set, regardless
of the value.
// โ Get first div that has `title` attribute set to `first` const el1 = document.querySelector('div[title="first"]'); console.log(el1); // ๐๏ธ div.box // โ Get first element that has a `title` attribute set const el2 = document.querySelector('[title]'); console.log(el2); // ๐๏ธ div.box
The first example looks for a div
element that has the title
attribute set
to first
.
Any span
or p
elements that have the attribute set would not match the
query.
The second example looks for a DOM element that has the title
attribute set.
The attribute could be set to any value for the condition to be satisfied.
querySelectorAll
method instead of querySelector
.To get a DOM element by partially matching an attribute value, use the
querySelector
method with a selector that matches an attribute whose value
starts with, ends with or contains a specific string.
// โ Get element where `title` STARTS with "fir" const el1 = document.querySelector('[title^="fir"]'); // โ Get element where `title` ENDS with "st" const el2 = document.querySelector('[title$="st"'); // โ Get element where `title` CONTAINS "first" const el3 = document.querySelector('[title*="first"');
querySelectorAll
method instead of querySelector
.The first example selects a DOM element where the value of the title
attribute
starts with fir
.
const el1 = document.querySelector('[title^="fir"]');
^
symbol, which has the same meaning when used in regular expressions.The second example selects a DOM element where the value of the title
attribute ends with st
.
const el2 = document.querySelector('[title$="st"');
The third example selects a DOM element where the value of the title
attribute
contains first
.
const el3 = document.querySelector('[title*="first"');
The string first
could be located anywhere in the title
attribute, as long
as it is contained, the condition would be met.
You could also prefix the selector with a specific type of element you want to match to narrow down the results.
// โ Get `div` where `title` STARTS with "fir" const el1 = document.querySelector('div[title^="fir"]');
The example selects a div
element where the value of the title
attribute
starts with fir
.
You can select elements by an attribute name by passing a selector with the
attribute's name to the querySelectorAll()
method.
The querySelectorAll
method will return a collection of the elements that have
the provided attribute set.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div title="first">Box 1</div> <div title="second">Box 2</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// โ All elements with `title` attribute const elements = document.querySelectorAll('[title]'); console.log(elements); // ๐๏ธ [div, div] // โ First element with `title` attribute const element = document.querySelector('[title]'); console.log(element); // ๐๏ธ div
The first example uses the
document.querySelectorAll() method to
get a collection of all elements that have the title
attribute set.
The second example uses the
document.querySelector() method
to get the first element in the document that has the title
attribute set.
In short, if you need a collection of elements, use the
document.querySelectorAll
method, and if you need the first element that
matches the provided selector, use the document.querySelector
method.
div
elements by a specific attribute name.// โ Get `div` elements with `title` attribute const elements = document.querySelectorAll('div[title]'); console.log(elements); // ๐๏ธ [div, div]
The code snippet targets only div
elements that have the title
attribute
set.
Any span
or p
elements that have the attribute set would not match the
query.
An alternative approach is to select the elements from the DOM and iterate over
the NodeList
checking if each element has a specific attribute set.
const elements = document.getElementsByTagName('*'); const matches = []; for (const element of elements) { if (element.hasAttribute('title')) { matches.push(element); } } console.log(matches); // ๐๏ธ [div, div]
We used the getElementsByTagName
method to select all elements in the DOM.
div
or p
elements to work with a smaller collection.The next step is to use the
for...of loop to iterate over
the NodeList
.
On each iteration, we check if the element has the title
attribute set and if
it does, we push the element to the matches
array.
Use the querySelectorAll
method to get all DOM elements by an attribute.
The querySelectorAll
method will return a NodeList
containing the elements
that have the specified attribute set to the given value.
Here is the HTML code for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div class="box">Box 1</div> <div class="box">Box 2</div> <div class="box">Box 3</div> <!-- โ Your JS script here โ --> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// โ Get all where specific "attribute = value" const elements1 = document.querySelectorAll(`[class="box"]`); console.log(elements1); // ๐๏ธ [div.box, div.box, div.box] // ------------------------------------------------------- // โ Get only DIVs where specific "attribute = value" const elements2 = document.querySelectorAll(`div[class="box"]`); console.log(elements2); // ๐๏ธ [div.box, div.box, div.box] // ------------------------------------------------------- // โ Get all DIVs where specific attribute is set to any value const elements3 = document.querySelectorAll('div[class]'); console.log(elements3); // ๐๏ธ [div.box, div.box, div.box] // ------------------------------------------------------- // โ Get all elements where specific attribute is set to any value const elements4 = document.querySelectorAll('[class]'); console.log(elements4); // ๐๏ธ [div.box, div.box, div.box]
We passed different selectors to the
document.querySelectorAll() method to
get a NodeList
containing the specific DOM elements.
Note that the querySelectorAll
method returns a NodeList
, not an array. If
you need to convert the response to an array, pass it to the Array.from
method.
const arr = Array.from(document.querySelectorAll(`[class="box"]`));
The first example shows how to get all DOM elements where the class
attribute
is set to box
.
const elements1 = document.querySelectorAll(`[class="box"]`);
The attribute value has to be an exact match for the condition to be met, e.g.
it can't be box blue
, it has to be just box
.
In the second example, we get more specific and get all div
elements where the
class
attribute is equal to box
.
const elements2 = document.querySelectorAll(`div[class="box"]`);
This wouldn't match any span
or p
elements that have their class
attribute
set to box
.
NodeList
as a response from querySelectorAll
.In the third example, we get all div
elements that have the class
attribute
set on them.
const elements3 = document.querySelectorAll('div[class]');
As long as the div
element has the class
attribute set, even if the
attribute doesn't have a value, it would get selected and included in the return
value of querySelectorAll
.
The fourth example shows how to get all DOM elements that have the class
attribute set.
const elements4 = document.querySelectorAll('[class]');
This could be any DOM element, e.g. a div
, span
or p
, as long as it has
the class
attribute set.
Use the querySelectorAll
method to get all DOM elements by partially matching
an attribute.
// โ Get all where value of class starts with `bo` const elements1 = document.querySelectorAll(`[class^="bo"]`); console.log(elements1); // ๐๏ธ [div.box, div.box, div.box] // ------------------------------------------------------- // โ Get all where value of class ends with `ox` const elements2 = document.querySelectorAll(`[class$="ox"]`); console.log(elements2); // ๐๏ธ [div.box, div.box, div.box] // ------------------------------------------------------- // โ Get all where value of class contains `box` const elements3 = document.querySelectorAll(`[class*="box"]`); console.log(elements3); // ๐๏ธ [div.box, div.box, div.box]
The first example selects all DOM elements where the value of the class
attribute starts with bo
.
const elements1 = document.querySelectorAll(`[class^="bo"]`);
^
symbol, which has the same meaning when used with regular expressions.The second example selects all DOM elements where the value of the class
attribute ends with ox
.
const elements2 = document.querySelectorAll(`[class$="ox"]`);
The third example selects all DOM elements, where the value of the class
attribute contains the string box
.
const elements3 = document.querySelectorAll(`[class*="box"]`);
The string box
could be located anywhere in the value of the class attribute
for the condition to be met.
Note that you could also prefix the selector with a specific type of element you want to match to narrow down the results.
const elements1 = document.querySelectorAll(`div[class^="bo"]`);
The example above selects only div
elements that have a class
attribute set
and the attribute's value starts with bo
.
You can learn more about the related topics by checking out the following tutorials: