Attr: ownerElement property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

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

Value

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

Example

The following example displays the qualified name of the element of the two first elements, when we click on the appropriate button.

HTML

html
Click me


Qualified name of the owner element of the attribute xml:lang: None.

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.ownerElement.tagName.toLowerCase();
  });
  i++;
}

Specifications

Specification
DOM
# dom-attr-ownerelement

Browser compatibility