hasAttribute()
Introduction to the JavaScript hasAttribute()
method
hasAttribute()
methodTo check an element has a specified attribute or not, you use the hasAttribute()
method:
Parameters
The hasAttribute()
method accepts an argument that specifies the name of the attribute that you want to check.
Return value
The hasAttribute()
returns a Boolean value that indicates if the element has the specified attribute.
If the element contains an attribute, the hasAttribute()
returns true; otherwise, it returns false
.
JavaScript hasAttribute()
example
hasAttribute()
exampleSee the following example:
Output:
How it works:
Select the button with the id btnSend by using the
querySelector()
method.Check if the button has the disabled attribute by calling the
hasAttribute()
method on the button element.
Summary
Use the
hasAttribute()
method to check if an element contains a specified attribute.
Last updated