Observe attributes and reflect properties
Quick answer
Attributes represent markup configuration as strings or absence. A custom element may observe selected attributes and expose a more convenient typed JavaScript property.
How it works
Declare static observedAttributes = ["count"] (a static getter is also valid), and implement attributeChangedCallback(name, oldValue, newValue). Values are strings or null for absence. The callback can run on upgrade and before connectedCallback; render defensively. For a boolean property use hasAttribute and toggleAttribute; presence means true even when the text is "false". A reflecting property setter can update the attribute; the attribute callback should update internal UI, not repeatedly invoke the same setter. Validate numeric values and define defaults.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const changes=[];class LessonCount extends HTMLElement{static observedAttributes=["count"];attributeChangedCallback(name,oldValue,newValue){changes.push([name,oldValue,newValue]);}}customElements.define("lesson-count",LessonCount);const x=document.createElement("lesson-count");x.setAttribute("count","2");x.setAttribute("count","3");x.removeAttribute("count");console.log(JSON.stringify(changes));})()Expected console output:
[["count",null,"2"],["count","2","3"],["count","3",null]]The reaction supplies string values and null for absent attributes.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {class LessonToggle extends HTMLElement{get active(){return this.hasAttribute("active");}set active(v){this.toggleAttribute("active",Boolean(v));}}customElements.define("lesson-toggle",LessonToggle);const x=document.createElement("lesson-toggle");x.setAttribute("active","false");const before=x.active;x.active=false;console.log(before,x.hasAttribute("active"));})()Expected console output:
true falsePresence, not the literal string false, determines the boolean value.
Common mistakes
Do not treat all attribute text as typed values or mutate the same observed attribute recursively without a guard.
Practice
Implement a boolean active property and log count changes including removal.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Observe attributes and reflect properties — Knowledge Check
Apply the concept and reason about local example output.
What is newValue when an observed attribute is removed?
What does active="false" mean under the boolean-presence convention?
Why must attribute callbacks handle execution before connection?
What is the expected console output of Observe attributes and reflect properties example 1?
(async () => {const changes=[];class LessonCount extends HTMLElement{static observedAttributes=["count"];attributeChangedCallback(name,oldValue,newValue){changes.push([name,oldValue,newValue]);}}customElements.define("lesson-count",LessonCount);const x=document.createElement("lesson-count");x.setAttribute("count","2");x.setAttribute("count","3");x.removeAttribute("count");console.log(JSON.stringify(changes));})()What is the expected console output of Observe attributes and reflect properties example 2?
(async () => {class LessonToggle extends HTMLElement{get active(){return this.hasAttribute("active");}set active(v){this.toggleAttribute("active",Boolean(v));}}customElements.define("lesson-toggle",LessonToggle);const x=document.createElement("lesson-toggle");x.setAttribute("active","false");const before=x.active;x.active=false;console.log(before,x.hasAttribute("active"));})()