Custom Element Lifecycle
Custom element attributeChangedCallback()
Lifecycle callback on a custom element class, not a method of CustomElementRegistry. Runs for changes to attributes listed in static observedAttributes, including initialization during upgrade. Old or new values may be null. Avoid writing the same observed attribute recursively. Example result: status:ready.
Formal Syntax & Structure
html syntaxhtml
attributeChangedCallback(name, oldValue, newValue)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Changed attribute. |
| oldValue | string | null | Previous value. |
| newValue | string | null | New value. |
Return Value
undefined
Lifecycle callback return value is ignored.
Working Example
Example Snippethtml
<p> Result: <output id="result"></output></p><script> const result = document.getElementById("result"); (async () => { class DemoCard extends HTMLElement { static observedAttributes = ["status"]; attributeChangedCallback(name, oldValue, newValue) { this.textContent = name + ":" + newValue; } } customElements.define("demo-card", DemoCard); const el = document.createElement("demo-card"); el.setAttribute("status", "ready"); result.textContent = el.textContent; })().catch(error => { result.textContent = "Error: " + error.name; });</script>