• Sets the attribute name of the target element to the value. The value is coerced to a string.

    Type Parameters

    • 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 set.

    • value: undefined | null | AttributeValue

      Value to set for the attribute.

    Returns ElementOf<TN>

    Element representation of the specified target.

    elements!InvalidElementError if the specified target wasn't found.

    HTML (Before)

    <div id="example" role="slider">
    ...
    </div>

    Code

    let element = findElement("#example")!;

    element = setAttribute(element, "aria-label", "Click me");

    HTML (After)

    <div
    id="example"
    role="slider"
    aria-label="Click me"
    >
    ...
    </div>