Type of value to return.
Tag name of the Element representation of target
.
Element, EventTarget, or CSS selector.
Name of the attribute to get.
Value of type V
or null
if not found.
We're returning null
, rather than undefined
to match the
Element.getAttribute API.
elements!InvalidElementError
if the specified target
wasn't found.
HTML
<div
id="example"
role="slider"
aria-valuemax="30"
aria-label="Example"
aria-disabled="false"
>
...
</div>
Code
const element = findElement("#example")!;
getAttribute(element, "aria-label");
// "Example"
getAttribute(element, "aria-valuemax");
// 30
getAttribute(element, "aria-disabled");
// false
Attempts to get the attribute
name
from thetarget
. If the value is found, it is coerced to a boolean if"true"
or"false"
, a number if numeric, or the string value if a string. If not found, returnsnull
.