--- title: Element.hasAttribute() slug: Web/API/Element/hasAttribute translation_of: Web/API/Element/hasAttribute ---
El método Elemento.hasAttribute() devuelve un valor Booleano indicando si el elemento tiene el atributo especificado o no.
var resultado = elemento.hasAttribute(nombre);
resultadotrue o false).nombrevar foo = document.getElementById("foo");
if (foo.hasAttribute("bar")) {
// hacer algo
}
;(function(prototype) {
prototype.hasAttribute = prototype.hasAttribute || function(name) {
return !!(this.attributes[name] &&
this.attributes[name].specified);
}
})(Element.prototype);
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('DOM WHATWG', '#dom-element-hasattribute', 'Element.hasAttribute()')}} | {{Spec2('DOM WHATWG')}} | From {{SpecName('DOM3 Core')}}, moved from {{domxref("Node")}} to {{domxref("Element")}} |
| {{SpecName('DOM3 Core', 'core.html#ID-ElHasAttr', 'Element.hasAttribute()')}} | {{Spec2('DOM3 Core')}} | No change from {{SpecName('DOM2 Core')}} |
| {{SpecName('DOM2 Core', 'core.html#ID-ElHasAttr', 'Element.hasAttribute()')}} | {{Spec2('DOM2 Core')}} | Initial definition. |