HTML DOM hasAttribute()Method
Example
Check if the button element has an onclick attribute:
document.getElementsByTagName("BUTTON")[0].hasAttribute("onclick");
Output:
true
Try it Yourself »
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 usesetAttribute()to add a new attribute, or modify an existing attribute of 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
| Parameter | 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 |
|---|
Other Extensions
Element Object