W3cubDocs

/Web APIs

Attr

The Attr interface represents one of an element's attributes as an object. In most situations, you will directly retrieve the attribute value as a string (e.g., Element.getAttribute()), but certain functions (e.g., Element.getAttributeNode()) or means of iterating return Attr instances.

EventTarget Node Attr

The core idea of an object of type Attr is the association between a name and a value. An attribute may also be part of a namespace and, in this case, it also has a URI identifying the namespace, and a prefix that is an abbreviation for the namespace.

The name is deemed local when it ignores the eventual namespace prefix and deemed qualified when it includes the prefix of the namespace, if any, separated from the local name by a colon (:). We have three cases: an attribute outside of a namespace, an attribute inside a namespace without a prefix defined, an attribute inside a namespace with a prefix:

Attribute Namespace name Namespace prefix Attribute local name Attribute qualified name
myAttr none none myAttr myAttr
myAttr mynamespace none myAttr myAttr
myAttr mynamespace myns myAttr myns:myAttr

Note: This interface represents only attributes present in the tree representation of the Element, being a SVG, an HTML or a MathML element. It doesn't represent the property of an interface associated with such element, such as HTMLTableElement for a <table> element. (See this article for more information about attributes and how they are reflected into properties.)

Instance properties

This interface also inherits the properties of its parent interfaces, Node and EventTarget.

localName Read only

A string representing the local part of the qualified name of the attribute.

name Read only

The attribute's qualified name. If the attribute is not in a namespace, it will be the same as localName property.

namespaceURI Read only

A string representing the URI of the namespace of the attribute, or null if there is no namespace.

ownerElement Read only

The Element the attribute belongs to.

prefix Read only

A string representing the namespace prefix of the attribute, or null if a namespace without prefix or no namespace are specified.

specified Read only Deprecated

This property always returns true.

value

The attribute's value, a string that can be set and get using this property.

Instance methods

This interface has no specific methods, but inherits the methods of its parent interfaces, Node and EventTarget.

Specifications

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 4.4 18 4 10.1 1 1.0
localName 1 12 1 9 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
name 1 12 1 6 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
namespaceURI 1 12 1 9 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
ownerElement 1 12 1 8 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
prefix 1 12 1 9 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
specified 1 12 1 5.5 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
value 1 12 1 6 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Attr