Categories
JavaScript Answers

How to get HTML element via aria label with JavaScript?

Spread the love

To get HTML element via aria label with JavaScript, we use the querySelector or querySelectorAll methods.

For instance, we write

const element = document.querySelector('[aria-label="Message Body"]');

to select the first element with the aria-label attribute set to Message Body.

Likewise, we write

const list = document.querySelectorAll('[aria-label="Message Body"]');

to select all the elements with the aria-label attribute set to Message Body returned in a node list.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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