Element representation of target.
Element, EventTarget, or CSS selector.
Name of the attribute to check for.
Optionalvalue: AttributeValueOptional attribute value to check for.
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
Checks if the
targethas the attributename. If avalueis specified, checks that the values match.