Attr
The Attr
interface represents one of a DOM element's attributes as an object. In most DOM methods, you will directly retrieve the attribute as a string (e.g., Element.getAttribute()
), but certain functions (e.g., Element.getAttributeNode()
) or means of iterating return Attr
types.
Warning: Starting in Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4), a number of deprecated properties and methods output warning messages to the console. You should revise your code accordingly. See Deprecated properties and methods for a complete list.
Properties
-
name
Read only -
The attribute's name.
-
namespaceURI
Read only -
A
DOMString
representing the namespace URI of the attribute, ornull
if there is no namespace. -
localName
Read only -
A
DOMString
representing the local part of the qualified name of the attribute. -
prefix
Read only -
A
DOMString
representing the namespace prefix of the attribute, ornull
if no prefix is specified. -
ownerElement
Read only -
The element holding the attribute.
Note: DOM Level 4 removed this property. The assumption was that since you get an
Attr
object from anElement
, you should already know the associated element. As that doesn't hold true in cases likeAttr
objects being returned byDocument.evaluate
, the DOM Living Standard reintroduced the property.Gecko outputs a deprecation note starting from Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4). This note was removed again in Gecko 49.0 (Firefox 49.0 / Thunderbird 49.0 / SeaMonkey 2.46).
-
specified
Read only -
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
-
The attribute's value.
Deprecated properties and methods
The following properties have been deprecated. Where available, the appropriate replacement is noted.
attributes
-
This property now always returns
NULL
. -
childNodes
-
This property now always returns an empty
NodeList
. -
firstChild
-
This property now always returns
NULL
. -
isId
Read only -
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 DTD or other schema description.
-
lastChild
-
This property now always returns
NULL
. nextSibling
-
This property now always returns
NULL
. nodeName
-
Use
Attr.name
instead. nodeType
-
This property now always returns 2 (
ATTRIBUTE_NODE
). nodeValue
-
Use
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.
parentNode
-
This property now always returns
NULL
. previousSibling
-
This property now always returns
NULL
. -
schemaTypeInfo
Read only -
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
Document.normalizeDocument
, this property may not be reliable if the node was moved. specified
-
This property now always returns
true
. textContent
-
Use
Attr.value
instead.
The following methods have been deprecated:
-
appendChild()
-
Modify the value of
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
Element.setAttribute()
instead. createAttributeNS()
-
Use
Element.setAttributeNS()
instead. getAttributeNode()
-
Use
Element.getAttribute()
instead. getAttributeNodeNS()
-
Use
Element.getAttributeNS()
instead. -
hasAttributes()
-
This method now always returns false.
hasChildNodes()
-
This method now always returns false.
insertBefore()
-
Modify the value of
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
Element.removeAttribute()
instead. -
removeChild()
-
Modify the value of
Attr.value
instead. -
replaceChild()
-
Modify the value of
Attr.value
instead. setAttributeNode()
-
Use
Element.setAttribute()
instead. setAttributeNodeNS()
-
Use
Element.setAttributeNS()
instead.
Specifications
Specification |
---|
DOM Standard (DOM) # interface-attr |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Attr |
1 |
12 |
1 |
5.5 |
8 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
localName |
1 |
12 |
1 |
9 |
≤12.1 |
1 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
name |
1 |
12 |
1 |
6 |
≤12.1 |
1 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
namespaceURI |
1 |
12 |
1 |
9 |
≤12.1 |
1 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
ownerElement |
1 |
12 |
1 |
8 |
≤12.1 |
1 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
prefix |
1 |
12 |
1 |
9 |
≤12.1 |
1 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
specified |
1 |
12 |
1 |
5.5 |
≤12.1 |
1 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
value |
1 |
12 |
1 |
6 |
≤12.1 |
1 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Attr