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

javascriptelement.hasAttribute(attribute name);

Example

Let's check for the presence of the value attribute on an element:

html<input id="elem" value="abcde">
javascriptlet elem = document.querySelector('#elem');
console.log(elem.hasAttribute('value'));

The code execution result:

javascripttrue

Example

And now there is no the value attribute:

html<input id="elem">
javascriptlet elem = document.querySelector('#elem');
console.log(elem.hasAttribute('value'));

The code execution result:

javascriptfalse

See also