• Attempts to get the attribute name from the target. 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, returns null.

    Type Parameters

    • V extends AttributeValue = string

      Type of value to return.

    • TN extends TagName = "*"

      Tag name of the Element representation of target.

    Parameters

    • target: null | Target<TN>

      Element, EventTarget, or CSS selector.

    • name: AttributeName<TN>

      Name of the attribute to get.

    Returns V | null

    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