Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home ยป Get Element by aria-label using JavaScript

Get Element by aria-label using JavaScript

December 10, 2023 by Source Freeze Leave a Comment

When we talk about get elements by aria-label using JavaScript, there are several methods at our disposal. Let’s see into three primary methods and explore the first one in detail but first What is an aria-label?

Get Element by aria-label using JavaScript

What is aria-label?

In the world of websites and apps, elements like buttons, images, or links often need labels to tell us what they do. But sometimes, these elements don’t have visible text labels. That’s where “aria-label” comes in.

Think of “aria-label” as a special tag we can give to things on a webpage to describe what they are or what they do. It’s like a hidden label that helps computers and assistive technologies, like screen readers, understand what an element is all about.

For instance, imagine a button with an icon instead of words. The “aria-label” acts like a secret message attached to that button, telling anyone who can’t see the icon what it’s there for.

Now let’s explore how to get Element by aria-label using JavaScript using the following methods:

  • Using querySelector for an Exact Match
  • Partial Match of aria-label Value
  • Narrowing Down with Specific Element Types

Using querySelector for an Exact Match

To begin with, one effective way to target an element with a precise aria-label is by using the querySelector method. This method fetches the first element in the document that matches the provided selector.

Let’s consider an example scenario:

<body>
  <button aria-label="Dismiss">X</button>
  <button aria-label="Submit">Submit</button>
</body>

If we wish to retrieve the button element with the aria-label attribute set to “Dismiss,” here’s how we accomplish it:

const dismissButton = document.querySelector('[aria-label="Dismiss"]');
console.log(dismissButton); // ๐Ÿ‘‰๏ธ button element with aria-label="Dismiss"

This code snippet uses the attribute selector [aria-label="Dismiss"] within querySelector to pinpoint the button element precisely.

By employing this method, we ensure we fetch the exact element matching the specified aria-label attribute value.

Partial Match of aria-label Value

Sometimes, we might need to find elements based on a partial match of their aria-label attribute. JavaScript provides selectors that cater to these scenarios:

  1. Starts with (^=)
  2. Ends with ($=)
  3. Contains (*=)

Suppose we have the following HTML elements:

<div aria-label="Notification: Error">Error Message</div>
<button aria-label="Close Notification">X</button>
<button aria-label="Open Menu">Menu</button>

Starts with (^=)

Let’s say we want to target elements whose aria-label starts with “Notif.” We use the ^= selector to accomplish this:

const notificationElement = document.querySelector('[aria-label^="Notif"]');
console.log(notificationElement); // ๐Ÿ‘‰๏ธ div with aria-label="Notification: Error"

By using [aria-label^="Notif"], we locate elements where the aria-label attribute starts with “Notif.”

Ends with ($=) and Contains (*=)

Similarly, if we aim to find elements where the aria-label attribute ends with or contains specific strings, we use the $= and *= selectors, respectively.

Let’s move on to the third method:

Narrowing Down with Specific Element Types

In addition to targeting elements by their aria-label attributes, we can further refine our selection by specifying the element type.

For instance, consider the following HTML snippet:

<button aria-label="Close Notification">X</button>
<div aria-label="Open Menu">Menu</div>
<button aria-label="Submit">Submit</button>

Suppose we’re interested in specifically selecting button elements based on their aria-label attributes.

Narrowing Down Buttons by aria-label

To achieve this, we use a selector that combines the button element type with the aria-label attribute condition:

const closeButton = document.querySelector('button[aria-label="Close Notification"]');
console.log(closeButton); // ๐Ÿ‘‰๏ธ button with aria-label="Close Notification"

By employing this selector (button[aria-label="Close Notification"]), we precisely target the button element with the specified aria-label.

This method enables us to not only focus on aria-label attributes but also narrow down the elements by their specific types, ensuring a more refined selection process

Conclusion

get elements by their aria-label attributes using javascript provide a powerful way to access specific components, especially in scenarios where elements might lack distinctive identifiers.

Throughout this discussion, we’ve uncovered three fundamental methods:

  1. Exact Match using querySelector: Precisely fetches elements based on an exact aria-label attribute value.
  2. Partial Matching(^=, $=, *=): Enables searching for elements with aria-label attributes that start with, end with, or contain specific strings.
  3. Narrowing Down with Specific Element Types: Allows refining element selection by combining aria-label conditions with specific element types.

By leveraging these methods, we can efficiently navigate and interact with elements in the Document Object Model (DOM), enhancing accessibility and functionality within web applications.

Thanks for stopping by! Check out more from Source Freeze here!

Filed Under: javascript Tagged With: beginner, javascript, web developer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Comments

    Tags

    beginner colon cross-platform es6 function html ionic iOS javascript mobile application development nextjs objective-c swift switch ternary typescript uiwebview Visual Studio plugin web developer

    Copyright © 2025 Source Freeze