Tag name of the Element representation of target
.
Element, EventTarget, or CSS selector.
Name of the attribute to check for.
Optional
value: 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
target
has the attributename
. If avalue
is specified, checks that the values match.