Web APIs
Element.hasAttribute()
Check for attribute presence independently of its value, including an empty attribute. Example console output: true false.
Formal Syntax & Structure
javascript syntaxjavascript
element.hasAttribute(name)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Attribute name. |
Return Value
boolean
Whether the attribute is present.
Working Example
Example Snippetjavascript
const p=document.createElement("p"); p.setAttribute("data-ready",""); console.log(p.hasAttribute("data-ready"),p.hasAttribute("data-other"));