HTML DOM Element hasAttribute()
Examples
Does "myButton" have an onclick attribute:
let answer = myButton.hasAttribute("onclick");
Try it Yourself »
If an <a> element has a target attribute, change the value to "_self":
if (element.hasAttribute("target")) {
element.setAttribute("target", "_self");
}
Try it Yourself »
Definition and Usage
The hasAttribute()
method returns true
if the attribute exists, otherwise false
.
Syntax
element.hasAttribute(name)
Parameters
Parameter | Description |
name | Required. The name of the attribute. |
Return Value
Type | Description |
Boolean | true if the element has the attribute,
otherwise false . |
Browser Support
element.hasAttribute()
is a DOM Level 2 (2001) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |