Last updated: Mar 4, 2024
Reading timeยท3 min
To get an element by aria-label, pass a selector that targets the specific
aria-label
value to the querySelector()
method.
The querySelector
method returns the first element in the document that
matches the provided selector.
Here is the HTML for the examples in the article.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <button aria-label="Close">X</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// โ Get first element with aria-label = "Close" const element = document.querySelector('[aria-label="Close"]'); console.log(element); // ๐๏ธ button // โ Get all elements with aria-label = "Close" const elements = document.querySelectorAll( '[aria-label="Close"]', ); console.log(elements); // ๐๏ธ [button]
aria-label
attribute value, scroll down to the next subheading.The first example uses the
document.querySelector() method
to get the first element in the document that has an aria-label
attribute set
to Close
.
// โ Get first element with aria-label = "Close" const element = document.querySelector('[aria-label="Close"]'); console.log(element); // ๐๏ธ button
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 an
aria-label
attribute set to Close
.
// โ Get all elements with aria-label = "Close" const elements = document.querySelectorAll('[aria-label="Close"]'); console.log(elements); // ๐๏ธ [button]
button
or div
elements that have the aria-label
attribute or selecting elements that have the attribute set, regardless of the value.// โ select `button` with aria-label = "Close" const el1 = document.querySelector('button[aria-label="Close"]'); console.log(el1); // ๐๏ธ button // โ select any element with aria-label attribute set const el2 = document.querySelector('[aria-label]'); console.log(el2); // ๐๏ธ button
The first example looks for a button
element that has the aria-label
attribute set to Close
.
Any div
or p
elements that have the attribute set would not satisfy the
condition.
The second example looks for a DOM element that has the aria-label
attribute
set.
The attribute could be set to any value for the condition to be met.
querySelectorAll
method instead of querySelector
.To get a DOM element by partially matching the aria-label
value, use the
querySelector
method with a selector that matches the attribute where the
value starts with, ends with, or contains a specific string.
// โ Get element where aria-label STARTS with Clo const el1 = document.querySelector('[aria-label^="Clo"]'); // โ Get element where aria-label ENDS with ose const el2 = document.querySelector('[aria-label$="ose"]'); // โ Get element where aria-label CONTAINS Close const el3 = document.querySelector('[aria-label*="Close"]');
querySelectorAll
method instead of querySelector
.The first example selects a DOM element where the value of the aria-label
attribute starts with Clo
.
const el1 = document.querySelector('[aria-label^="Clo"]');
^
symbol, which has the same meaning when used in regular expressions.The second example selects a DOM element where the value of the aria-label
attribute ends with ose
.
const el2 = document.querySelector('[aria-label$="ose"]');
The third example selects a DOM element where the value of the aria-label
attribute contains Close
.
const el3 = document.querySelector('[aria-label*="Close"]');
The string Close
could be located anywhere in the aria-label
attribute, as
long as it is contained, the condition would be satisfied.
You could also prefix the selector with a specific type of element you want to match to narrow down the results.
// โ Get `button` where aria-label STARTS with Clo const el1 = document.querySelector('button[aria-label^="Clo"]');
The example selects a button
element where the value of the aria-label
attribute starts with Clo
.
You can learn more about the related topics by checking out the following tutorials: