Document.registerElement()

Note:document.registerElement() is deprecated in favor of customElements.define().

Draft
This page is not complete.

The document.registerElement() method registers a new custom element in the browser and returns a constructor for the new element.

Note: This is an experimental technology. The browser you use it in must support Web Components. See Enabling Web Components in Firefox.

Syntax

JavaScript
var constructor = document.registerElement(tag-name, options);

Parameters

tag-name
The name of the custom element. The name must contain a dash (-), for example my-tag.
options Optional

An object with properties prototype to base the custom element on, and extends, an existing tag to extend. Both of these are optional.

Example

Here is a very simple example:

JavaScript
var Mytag = document.registerElement('my-tag');

Now the new tag is registered in the browser. The Mytag variable holds a constructor that you can use to create a my-tag element in the document as follows:

JavaScript
document.body.appendChild(new Mytag());

This inserts an empty my-tag element that will be visible if you use the browser's developer tools. It will not be visible if you use the browser's view source capability. And it won't be visible in the browser unless you add some content to the tag. Here is one way to add content to the new tag:

JavaScript
var mytag = document.getElementsByTagName("my-tag")[0];
mytag.textContent = "I am a my-tag element.";

Specifications

Specification Status Comment
Custom Elements Working Draft Initial definition

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 35 31[1] No support 25 No support
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 4.4.4 31[1] No support 25 No support

[1] This API is implemented behind a preference.

See Also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/document/registerelement

API DOM Experimental Method Reference web components Web Components