Register autonomous custom elements
Quick answer
Custom elements let you define reusable HTML elements with JavaScript classes. An autonomous custom element extends HTMLElement and uses a registered name containing a hyphen.
How it works
Call customElements.define(name, constructor) once per name and constructor in the registry. Use a valid lowercase custom element name with a hyphen, excluding reserved names. customElements.get(name) retrieves a registered constructor or undefined. whenDefined(name) returns a promise that resolves to the constructor once defined. Existing matching elements can be upgraded after registration. Keep constructors minimal, call super before accessing this, and avoid reading attributes or children during construction. This chapter uses autonomous elements in the global registry; customized built-ins and scoped registries require separate compatibility review.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {class LessonStatus extends HTMLElement{};customElements.define("lesson-status",LessonStatus);const el=document.createElement("lesson-status");console.log(el instanceof LessonStatus);})()Expected console output:
trueThe newly created element uses the registered class.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const waiting=customElements.whenDefined("lesson-chip");class LessonChip extends HTMLElement{};customElements.define("lesson-chip",LessonChip);console.log((await waiting)===customElements.get("lesson-chip"));})()Expected console output:
trueThe promise yields the constructor after definition.
Common mistakes
Do not redefine the same registered name or use names such as panel without a hyphen. Registration does not provide accessibility semantics automatically.
Practice
Define a lesson-status element and use get and whenDefined to inspect its registration.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Register autonomous custom elements — Knowledge Check
Apply the concept and reason about local example output.
Why must an autonomous custom element use a valid name containing a hyphen?
What does customElements.get return for a registered name?
What does whenDefined resolve to?
What is the expected console output of Register autonomous custom elements example 1?
(async () => {class LessonStatus extends HTMLElement{};customElements.define("lesson-status",LessonStatus);const el=document.createElement("lesson-status");console.log(el instanceof LessonStatus);})()What is the expected console output of Register autonomous custom elements example 2?
(async () => {const waiting=customElements.whenDefined("lesson-chip");class LessonChip extends HTMLElement{};customElements.define("lesson-chip",LessonChip);console.log((await waiting)===customElements.get("lesson-chip"));})()