MDN Web Docs

Value

A boolean value that is true if the node is connected to its relevant context object, and false if not.

Note: An Attr node always returns false for isConnected, even when its ownerElement is connected. This is because, even though an attribute is associated with an element via ownerElement, it is not part of the node tree — it has no parent node, and it is its own root node. Since isConnected is only true when a node's root is a document, an Attr node is never considered connected.

Examples

Standard DOM

A standard DOM example:

js

let test = document.createElement("p");
console.log(test.isConnected); // Returns false
document.body.appendChild(test);
console.log(test.isConnected); // Returns true

Shadow DOM

A shadow DOM example:

js

// Create a shadow root
const shadow = this.attachShadow({ mode: "open" });
// Create some CSS to apply to the shadow DOM
const style = document.createElement("style");
console.log(style.isConnected); // returns false
style.textContent = `
.wrapper {
  position: relative;
}
.info {
  font-size: 0.8rem;
  width: 200px;
  display: inline-block;
  border: 1px solid black;
  padding: 10px;
  background: white;
  border-radius: 10px;
  opacity: 0;
  transition: 0.6s all;
  positions: absolute;
  bottom: 20px;
  left: 10px;
  z-index: 3
}
`;
// Attach the created style element to the shadow DOM
shadow.appendChild(style);
console.log(style.isConnected); // Returns true

Specifications

Specification
DOM
# ref-for-dom-node-isconnected①

Browser compatibility

Help improve MDN

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Read the original on developer.mozilla.org ↗