Value
A string representing the local part of the attribute's qualified name.
Example
The following example displays the local name of the first attribute of the two first elements, when we click on the appropriate button. The <svg> element is XML and supports namespaces leading to the local name (lang) to be different from the qualified name xml:lang. The <label> element is HTML, that doesn't support namespaces, leading to a local name and the qualified name to be both xml:lang.
HTML
html
<svg xml:lang="en-US" class="struct" height="1" width="1">Click me</svg>
<label xml:lang="en-US" class="struct"></label>
<p>
<button>Show value for <svg></button>
<button>Show value for <label></button>
</p>
<p>
Local part of the attribute <code>xml:lang</code>:
<output id="result">None.</output>
</p>
JavaScript
js
const elements = document.querySelectorAll(".struct");
const buttons = document.querySelectorAll("button");
const outputEl = document.querySelector("#result");
let i = 0;
for (const button of buttons) {
const element = elements[i];
button.addEventListener("click", () => {
const attribute = element.attributes[0];
outputEl.value = attribute.localName;
});
i++;
}
Specifications
| Specification |
|---|
| DOM # dom-attr-localname |
Browser compatibility
See also
- The properties
Attr.name, returning the qualified name of the attribute, andAttr.prefix, the namespace prefix. - The
Element.localName()property, returning the local name of anElement.