

The result is less code, modular code, and more reuse in our apps.

It brings a web standards-based way to create reusable components using nothing more than vanilla JS/HTML/CSS. The API is the foundation of web components. With Custom Elements, web developers can create new HTML tags, beef-up existing HTML tags, or extend the components other developers have authored. Creating an element that uses Shadow DOM.
Custom html tags code#
I’ll show you the code first and then walk you through it. This is where we will create the and elements that are responsible for displaying the product. Next we need to set up a function for createdCallback. Var XProductProto = Object.create(HTMLElement.prototype) Create a new object based of the HTMLElement prototype We’ll start by create a new prototype object based off of HTMLElement.prototype. The information needed to display the product is contained within data- attributes on the custom element. The idea here is that a web developer can easily create new products by adding a single line of HTML to their markup. In this section we’re going to look at an example of how you can use custom elements and Shadow DOM to create an interface component for displaying products in a web store. Using custom elements and shadow DOM to create reusable product cards. This makes it really easy to create reusable interface components. The true power of custom elements becomes clear when you think about how they can be used alongside Shadow DOM. You can then define your custom methods on this new object, as shown below. var XTreehouseProto = Object.create(HTMLElement.prototype) Passing HTMLElement.prototype to this method will create an object with the standard set of methods and properties available to HTML elements. This can be done using the Object.create() method. To do this, start by creating a new JavaScript object. You can define a JavaScript API for your custom element that consists of a number of methods and properties. Creating a JavaScript API For Your Element

This also means that you won’t encounter problems if a new HTML element is introduced that uses the same name as your custom element. The name of your custom element must contain a dash (-) so when the browser parses your code, it can determine between standard and custom HTML elements. This would add the following HTML to the end of the element: var XTreehouseElement = document.customElements('x-treehouse') ĭ(new XTreehouseElement()) In the following example we simply create a new HTML element called and then add it to the page.

This should be passed as the name of your custom element along with an (optional) object that defines the API. The () method is used to create a custom HTML element.
Custom html tags how to#
In this article you’re going to learn how to create your own HTML elements and define a JavaScript API for them. This can be useful when building interfaces with reused components throughout an application.įree trial on Treehouse: Do you want to learn more about HTML and front-end programming? Click here to try a free trial on Treehouse. These allow you to create your own HTML elements along with their own JavaScript API. An exciting feature of the HTML specification is creating custom HTML elements.
