Table of Contents

Class Attr

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

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., GetAttribute(string)), but some cases may require interacting with Attr instances (e.g., GetAttributeNode(string)).

[Value("Attr")]
public class Attr : Node
Inheritance
Attr
Inherited Members

Remarks

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 {{HTMLElement("table")}} element. (See {{Glossary("Attribute", "this article")}} for more information about attributes and how they are reflected into properties.)

-Other nodes are CDATASection, CharacterData, Comment, Document, Element, ProcessingInstruction, and Text.

See also on MDN

Constructors

Attr()

public Attr()

Properties

LocalName

The read-only localName property of the Attr interface returns the local part of the qualified name of an attribute, that is the name of the attribute, stripped from any namespace in front of it. For example, if the qualified name is xml:lang, the returned local name is lang, if the element supports that namespace.

[Value("localName")]
public string LocalName { get; }

Property Value

string

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

Remarks

The local name is always in lower case, whatever case at the attribute creation.

NOTE

HTML only supports a fixed set of namespaces on SVG and MathML elements. These are xml (for the xml:lang attribute), xlink (for the xlink:href, xlink:show, xlink:target and xlink:title attributes) and xpath.

That means that the local name of an attribute of an HTML element is always be equal to its qualified name: Colons are treated as regular characters. In XML, like in SVG or MathML, the colon denotes the end of the prefix and what is before is the namespace; the local name may be different from the qualified name.

-The properties Name, returning the qualified name of the attribute, and Prefix, the namespace prefix.
-The LocalName property, returning the local name of an Element.

See also on MDN

Name

The read-only name property of the Attr interface returns the qualified name of an attribute, that is the name of the attribute, with the namespace prefix, if any, in front of it. For example, if the local name is lang and the namespace prefix is xml, the returned qualified name is xml:lang.

[Value("name")]
public string Name { get; }

Property Value

string

A string representing the attribute's qualified name.

Remarks

The qualified name is always in lower case, whatever case at the attribute creation.

-The properties LocalName, returning the local part of the qualified name of the attribute, and Prefix, the namespace prefix.
-The TagName property, returning the qualified name of an Element.

See also on MDN

NamespaceURI

The read-only namespaceURI property of the Attr interface returns the namespace URI of the attribute,
or null if the element is not in a namespace.

[Value("namespaceURI")]
public string? NamespaceURI { get; }

Property Value

string

A string containing the URI of the namespace, or null if the attribute is not in a namespace.

Remarks

The namespace URI is set at the Attr creation and cannot be changed.
An attribute with a namespace can be created using SetAttributeNS(string?, string, string).

NOTE

An attribute does not inherit its namespace from the element it is attached to.
If an attribute is not explicitly given a namespace, it has no namespace.

The browser does not handle or enforce namespace validation per se. It is up to the JavaScript
application to do any necessary validation. Note, too, that the namespace prefix, once it
is associated with a particular attribute node, cannot be changed.

-The properties Name, returning the qualified name of the attribute, LocalName, the local part of the name, and Prefix, the namespace prefix.
-The NamespaceURI property, equivalent to this one but for an Element.
-The SetAttributeNS(string?, string, string) method, creating an attribute with a given namespace.

See also on MDN

OwnerElement

The read-only ownerElement property of the Attr interface returns the Element the attribute belongs to.

[Value("ownerElement")]
public Element? OwnerElement { get; }

Property Value

Element

The Element the attribute belongs to, or null if the attribute is not linked to an element.

Remarks

Prefix

The read-only prefix property of the Attr returns the namespace prefix of the attribute, or null if no prefix is specified.

[Value("prefix")]
public string? Prefix { get; }

Property Value

string

A string containing the prefix of the namespace the attribute belongs too. If none, it returns null.

Remarks

The prefix is always in lower case, whatever case is used at the attribute creation.

NOTE

Only XML supports namespaces. HTML does not. That means that the prefix of an attribute of an HTML element will always be null.

Also, only the xml (for the xml:lang attribute), xlink (for the xlink:href, xlink:show, xlink:target and xlink:title attributes) and xpath namespaces are supported, and only on SVG and MathML elements.

-The properties Name, returning the qualified name of the attribute, and LocalName, its local name.
-The Prefix property, returning the namespace prefix of an Element.

See also on MDN

Specified

IMPORTANT
Deprecated
The read-only specified property of the Attr interface always returns true.
[Value("specified")]
public bool Specified { get; }

Property Value

bool

Always returns true.

Remarks

Value

The value property of the Attr interface contains the value of the attribute.

[Value("value")]
public string Value { get; set; }

Property Value

string

A string representing the attribute value.

Remarks