HTML DOM hasAttribute() method
Example
Check whether the button element has the onclick attribute:
document.getElementsByTagName("BUTTON")[0].hasAttribute("onclick");
Output result:
true
Try it »
Definition and Usage
The hasAttribute() method is used to determine whether a specified attribute exists; if it exists it returns true, otherwise it returns false.
Tip: We can use the setAttribute() to add a new attribute, or modify an existing attribute in the element.
Browser support





All major browsers support the hasAttribute() method
Internet Explorer 8 and earlier IE versions do not support this method.
Syntax
element
.hasAttribute(
attributename
)
Parameters
| Parameters | Type | Description |
|---|---|---|
| attributename | String | Required. The name of the attribute you want to check. |
Return Value
| Type | Description |
|---|---|
| Boolean | Returns true if the element has the attribute, otherwise returns false. |
Technical Details
| DOM version | Core Level 2 Element Object |
|---|
Element object