• Checks if the target has the attribute name. If a value is specified, checks that the values match.

    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 check for.

    • Optionalvalue: AttributeValue

      Optional attribute value to check for.

    Returns boolean

    true if the attribute name is present and value matches (if specified).

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

    HTML

    <button id="example" aria-pressed="true" aria-label="Example">
    Example
    </button>

    Code

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

    hasAttribute(element, "aria-pressed");
    // true

    hasAttribute(element, "aria-pressed", "true");
    // false ("true" cannot be a string, must be the boolean value `true`)

    hasAttribute(element, "aria-label", "Example");
    // true