HTML Custom Elements: Registration and Lifecycle
Define an autonomous element
A custom element extends HTMLElement and is registered under a valid custom-element name containing a hyphen, such as study-note. A JavaScript class describes behavior; extends inherits the native element interface. The constructor must call super() before accessing this. connectedCallback runs when an instance connects to a document.
<study-note></study-note><script> class StudyNote extends HTMLElement { connectedCallback() { this.textContent = "Study HTML"; } } customElements.define("study-note", StudyNote);</script>The existing element upgrades and displays Study HTML.
Observe attributes
observedAttributes declares which attribute changes trigger attributeChangedCallback. It receives the name, old value and new value (null after removal). Avoid writing the same observed attribute from the callback without a guard, because that can cause recursion. The static getter belongs to the class, not individual instances.
<topic-label topic="HTML"></topic-label><script> class TopicLabel extends HTMLElement { static get observedAttributes() { return ["topic"]; } attributeChangedCallback(name, oldValue, newValue) { this.textContent = newValue ?? "No topic"; } } customElements.define("topic-label", TopicLabel); document.querySelector("topic-label").setAttribute("topic", "CSS");</script>The label reflects the new attribute value.
Connect and disconnect safely
Connection callbacks can run more than once. Use the same listener reference for addEventListener and removeEventListener. An arrow function stored on the instance retains this; cleanup prevents duplicate subscriptions or retained references. The constructor initializes state without reading child markup.
<study-counter></study-counter><script> class StudyCounter extends HTMLElement { constructor() { super(); this.count = 0; this.onReview = () => { this.textContent = String(++this.count); }; } connectedCallback() { window.addEventListener("review", this.onReview); } disconnectedCallback() { window.removeEventListener("review", this.onReview); } } customElements.define("study-counter", StudyCounter); const counter = document.querySelector("study-counter"); window.dispatchEvent(new Event("review")); counter.remove(); window.dispatchEvent(new Event("review")); document.body.append(counter); window.dispatchEvent(new Event("review"));</script>Only the two events dispatched while connected are counted.
Common mistakes and practice
Use a native button for interactive behavior instead of assuming a custom tag gains keyboard or accessibility semantics. A name can be registered only once per registry; use fresh documents during experiments.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Custom Elements: Registration and Lifecycle — Knowledge Check
Apply the components concepts taught in this lesson.
Which name is a valid autonomous custom-element name?
What is called on connection?
What determines observed attribute names?
Which callback is useful for removing global event listeners?
Does a custom tag automatically act as a native button?