🏷️ Get computed label
Get an element's computed label ("Accessible Name") from the Accessibility Tree
💡 Why?
Say you have a button which looks like this (this is invalid markup):
A span with an ID Inner text
What would a screen reader read?
Using the Accessibility Tree, screen readers and other assistive technologies figure out what they should read. In this case, even though the <button>
has inner text, the ARIA attributes take precedence. Even in that, the aria-labelledby
is higher in the accessibility order than aria-label
, but in this case, the element with the ID #ref1
doesn't exist, so we fall back to aria-label
.
Finally, the label is computed to be Aria label
.
💻 Usage
Install the package from NPM:
npm install get-computed-label
; const label = ;console;
You can also add this to the prototype, if you're into that:
HTMLElementprototype { return ;}; const image = document;console;
ℹ️ Further reading
- Accessible Name and Description Computation 1.1
- TODO Use computation method described above