--- title: CustomElementRegistry slug: Web/API/CustomElementRegistry tags: - API - CustomElementRegistry - Experimental - Interface - Landing - NeedsTranslation - Reference - TopicStub - Web Components - custom elements translation_of: Web/API/CustomElementRegistry --- <p>{{DefaultAPISidebar("Web Components")}}</p> <p><span class="seoSummary">The <strong><code>CustomElementRegistry</code></strong> interface provides methods for registering custom elements and querying registered elements. To get an instance of it, use the {{domxref("window.customElements")}} property. </span></p> <h2 id="Methods">Methods</h2> <dl> <dt>{{domxref("CustomElementRegistry.define()")}}</dt> <dd>Defines a new <a href="/en-US/docs/Web/Web_Components/Custom_Elements">custom element</a>.</dd> <dt>{{domxref("CustomElementRegistry.get()")}}</dt> <dd>Returns the constuctor for the named custom element, or <code>undefined</code> if the custom element is not defined.</dd> <dt>{{domxref("CustomElementRegistry.upgrade()")}}</dt> <dd>Upgrades a custom element directly, even before it is connected to its shadow root.</dd> <dt>{{domxref("CustomElementRegistry.whenDefined()")}}</dt> <dd>Returns an empty {{jsxref("Promise", "promise")}} that resolves when a custom element becomes defined with the given name. If such a custom element is already defined, the returned promise is immediately fulfilled.</dd> </dl> <h2 id="Examples">Examples</h2> <p>The following code is taken from our <a href="https://github.com/mdn/web-components-examples/tree/master/word-count-web-component">word-count-web-component</a> example (<a href="https://mdn.github.io/web-components-examples/word-count-web-component/">see it live also</a>). Note how we use the {{domxref("CustomElementRegistry.define()")}} method to define the custom element after creating its class.</p> <pre class="brush: js">// Create a class for the element class WordCount extends HTMLParagraphElement { constructor() { // Always call super first in constructor super(); // count words in element's parent element var wcParent = this.parentNode; function countWords(node){ var text = node.innerText || node.textContent return text.split(/\s+/g).length; } var count = 'Words: ' + countWords(wcParent); // Create a shadow root var shadow = this.attachShadow({mode: 'open'}); // Create text node and add word count to it var text = document.createElement('span'); text.textContent = count; // Append it to the shadow root shadow.appendChild(text); // Update count when element content changes setInterval(function() { var count = 'Words: ' + countWords(wcParent); text.textContent = count; }, 200) } } // Define the new element customElements.define('word-count', WordCount, { extends: 'p' });</pre> <div class="note"> <p>Note: The CustomElementRegistry is available through the {{domxref("Window.customElements")}} property.</p> </div> <h2 id="Specifications">Specifications</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName("HTML WHATWG", "custom-elements.html#customelementregistry", "CustomElementRegistry")}}</td> <td>{{Spec2("HTML WHATWG")}}</td> <td>Initial definition.</td> </tr> </tbody> </table> <h2 id="Browser_compatibility">Browser compatibility</h2> <p> </p> <p>{{Compat("api.CustomElementRegistry")}}</p> <p> </p>