{{APIRef("DOM")}}
This type represents a DOM element's attribute as an object. In most DOM methods, you will probably directly retrieve the attribute as a string (e.g., {{domxref("Element.getAttribute()")}}, but certain functions (e.g., {{domxref("Element.getAttributeNode()")}}) or means of iterating give Attr
types.
{{InheritanceDiagram}}
Warning: In DOM Core 1, 2 and 3,
Attr
inherited from {{domxref("Node")}}. This is no longer the case in DOM4. In order to bring the implementation of Attr
up to specification, work is underway to change it to no longer inherit from {{domxref("Node")}}. You should not be using any {{domxref("Node")}} properties or methods on Attr
objects. Starting in Gecko 7.0 {{geckoRelease("7.0")}}, the ones that are going to be removed output warning messages to the console. You should revise your code accordingly. See Deprecated properties and methods for a complete list.Properties
isId
{{readOnlyInline}} {{obsolete_inline}}- Indicates whether the attribute is an "ID attribute". An "ID attribute" being an attribute which value is expected to be unique across a DOM Document. In HTML DOM, "id" is the only ID attribute, but XML documents could define others. Whether or not an attribute is unique is often determined by a {{Glossary("DTD")}} or other schema description.
name
{{readOnlyInline}}- The attribute's name.
namespaceURI
{{readOnlyInline}}- A {{domxref("DOMString")}} representing the namespace URI of the attribute, or
null
if there is no namespace. localName
{{readOnlyInline}}- A {{domxref("DOMString")}} representing the local part of the qualified name of the attribute.
ownerElement
{{readOnlyInline}} {{deprecated_inline("7.0")}} {{obsolete_inline("29.0")}}- This property has been removed from Firefox 29. Since you can only get Attr objects from elements, you should already know the owner.
- Contrary to above claim, Document.evaluate can return Attr objects from an XPath, in which case you would not easily know the owner.
prefix
{{readOnlyInline}}- A {{domxref("DOMString")}} representing the namespace prefix of the attribute, or
null
if no prefix is specified. schemaTypeInfo
{{readOnlyInline}} {{obsolete_inline}}- The type information associated with this attribute. While the type information contained in this attribute is guaranteed to be correct after loading the document or invoking {{domxref("Document.normalizeDocument")}}, this property may not be reliable if the node was moved.
specified
{{readOnlyInline}}- This property always returns
true
. Originally, it returnedtrue
if the attribute was explicitly specified in the source code or by a script, andfalse
if its value came from the default one defined in the document's DTD. value
{{readOnlyInline}}- The attribute's value.
Deprecated properties and methods
The following properties have been deprecated. Where available, the appropriate replacement is provided.
attributes
- This property now always returns
NULL
. childNodes
- This property now always returns
NULL
. firstChild
- This property now always returns
NULL
. lastChild
- This property now always returns
NULL
. nextSibling
- This property now always returns
NULL
. nodeName
- Use {{domxref("Attr.name")}} instead.
nodeType
- This property now always returns 2 (
ATTRIBUTE_NODE
). nodeValue
- Use {{domxref("Attr.value")}} instead.
ownerDocument
- You shouldn't have been using this in the first place, so you probably don't care that this is going away.
ownerElement
- Since you get an
Attr
object from an {{domxref("Element")}}, you should already know the associated element. parentNode
- This property now always returns
NULL
. previousSibling
- This property now always returns
NULL
. specified
- This property now always returns
true
. textContent
- Use {{domxref("Attr.value")}} instead.
The following methods have been deprecated:
appendChild()
- Modify the value of {{domxref("Attr.value")}} instead.
cloneNode()
- You shouldn't have been using this in the first place, so you probably don't care that this is going away.
createAttribute()
- Use {{domxref("Element.setAttribute()")}} instead.
createAttributeNS()
- Use {{domxref("Element.setAttributeNS()")}} instead.
getAttributeNode()
- Use {{domxref("Element.getAttribute()")}} instead.
getAttributeNodeNS()
- Use {{domxref("Element.getAttributeNS()")}} instead.
hasAttributes()
{{obsolete_inline("21.0")}}- This method now always returns false.
hasChildNodes()
- This method now always returns false.
insertBefore()
- Modify the value of {{domxref("Attr.value")}} instead.
isSupported()
- You shouldn't have been using this in the first place, so you probably don't care that this is going away.
isEqualNode()
- You shouldn't have been using this in the first place, so you probably don't care that this is going away.
normalize()
- You shouldn't have been using this in the first place, so you probably don't care that this is going away.
removeAttributeNode()
- Use {{domxref("Element.removeAttribute()")}} instead.
removeChild()
- Modify the value of {{domxref("Attr.value")}} instead.
replaceChild()
- Modify the value of {{domxref("Attr.value")}} instead.
setAttributeNode()
- Use {{domxref("Element.setAttribute()")}} instead.
setAttributeNodeNS()
- Use {{domxref("Element.setAttributeNS()")}} instead.
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName("DOM4", "#interface-attr", "Attr")}} | {{Spec2("DOM4")}} | Moved namespaceURI , prefix and localName from {{domxref("Node")}} to this API and removed ownerElement , schemaTypeInfo and isId . |
{{SpecName("DOM3 Core", "core.html#ID-637646024", "Attr")}} | {{Spec2("DOM3 Core")}} | Initial definition |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}}[1] | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} [1] | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}}[1] |
[1] As of Chrome 45, this property no longer inherits from Node.