Web APIs
attributeChangedCallback()
Define this lifecycle method on a custom element to receive changes to observed attributes. On addition oldValue is null; on removal newValue is null. Example console output: [["data-count",null,"3"]].
Formal Syntax & Structure
javascript syntaxjavascript
attributeChangedCallback(name, oldValue, newValue)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Attribute name. |
| oldValue | string or null | Previous attribute value. |
| newValue | string or null | Updated attribute value. |
Return Value
undefined
Callback return value is ignored.
Working Example
Example Snippetjavascript
const changes=[]; class LessonBadge extends HTMLElement{static observedAttributes=["data-count"]; attributeChangedCallback(n,oldValue,newValue){changes.push([n,oldValue,newValue])}} customElements.define("lesson-badge",LessonBadge); const x=document.createElement("lesson-badge"); x.setAttribute("data-count","3"); console.log(JSON.stringify(changes));