Skip to content

Lifecycle Callbacks

Custom elements are not just inert tags — they have a lifecycle. The browser calls specific methods on your class at well-defined moments: when the element is created, when it is inserted into the document, when its attributes change, and when it is removed. Handling these moments correctly is the foundation of a well-behaved custom element.

This module covers four lifecycle callbacks:

  • constructor — element is created (class instantiated)
  • connectedCallback — element is inserted into a document
  • attributeChangedCallback — an observed attribute is added, changed, or removed
  • disconnectedCallback — element is removed from a document

It also covers upgrading — what happens when the browser encounters a custom element tag before the class is registered.

flowchart LR
  C["constructor
(element created)"]
  CC["connectedCallback
(inserted into DOM)"]
  AC["attributeChangedCallback
(attribute changed)
— fires each time —"]
  DC["disconnectedCallback
(removed from DOM)"]
  C --> CC
  CC --> AC
  AC --> AC
  CC --> DC
Web Component lifecycle callback sequence
CallbackWhen it fires
constructorElement is instantiated — via new or HTML parsing
connectedCallbackElement is inserted into any document (or shadow root)
attributeChangedCallbackAn observed attribute is set, changed, or removed
disconnectedCallbackElement is removed from the document

The attributeChangedCallback can fire multiple times — once per attribute change — and it fires both before and after the element is connected depending on when attributes are set.

If the browser encounters <my-widget> in the HTML before the class is registered, it creates a plain HTMLElement first. When customElements.define('my-widget', MyWidget) is later called, the browser upgrades those elements by running their constructor and, if they are already in the DOM, their connectedCallback. You can await an upgrade with customElements.whenDefined('my-widget').

The element below logs each lifecycle event it observes. Click Remove element to trigger disconnectedCallback, then re-add it to see connectedCallback fire again.

Which lifecycle callback fires when a custom element is inserted into the DOM?
What is required for attributeChangedCallback to receive notifications?
Which callback fires when a custom element is removed from the document?