Web APIs

attributeChangedCallback()

Try the free JavaScript compiler

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

NameTypeDescription
namestringAttribute name.
oldValuestring or nullPrevious attribute value.
newValuestring or nullUpdated 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));
JavaScript Reference