hasAttribute method
The hasAttribute method checks if
an element has a given attribute. If the
attribute is present, it will output
true, if not, it will output
false.
Syntax
element.hasAttribute(attribute name);
Example
Let's check for the presence of the
value
attribute on an element:
<input id="elem" value="abcde">
let elem = document.querySelector('#elem');
console.log(elem.hasAttribute('value'));
The code execution result:
true
Example
And now there is no the
value attribute:
<input id="elem">
let elem = document.querySelector('#elem');
console.log(elem.hasAttribute('value'));
The code execution result:
false
See also
-
the
getAttributemethod
that gets attributes -
the
setAttributemethod
that sets attributes -
the
removeAttributemethod
that removes attributes