--- 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);
resultado
true
o false)
.nombre
var 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. |
{{CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | 8.0 | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |