dom / latest / attr / prefix.html /

Attr.prefix

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

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.

Value

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

Example

HTML Content

<svg xml:lang="en-US" class="struct" height="1" width="1">Click me</svg>
<label xml:lang="en-US" class="struct"></label>

<button>Click me for &lt;svg&gt;</button>
<button>Click me for &lt;label&gt;</button>
<br><br>
Prefix of the attribute <code>xml:lang</code>: <output id="result"><i>None.</i></output>

JavaScript Content

const elements = document.getElementsByClassName("struct");
const buttons = document.getElementsByTagName("button");
const result  = document.querySelector("#result");

function handleEvent(element) {
  return function(e) {
    attribute = element.attributes[0];
    result.value = attribute.prefix;
  }
}

let i=0;
for (let button of buttons) {
  button.addEventListener('click', handleEvent(elements[i]));
  i++;
}

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
prefix
1
12
1
9
≤12.1
1
1
18
4
≤12.1
1
1.0

See also

© 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/prefix