Render Hello World Text with Custom Elements

Yonatan Kra
InstructorYonatan Kra
Share this video with your friends

Social Share Links

Send Tweet

Custom elements are fun technology. In this video, you will learn how to set one up and running in less than 2 minutes.

You'll learn how to create a Custom Web Element (that extends HTMLElement) that renders text to the browser and respond to a click event listener.

Instructor: [00:01] In order to create a custom element, we need to create a class. Inside our HDML file, we'll create a script tag. Inside we'll define a class. We'll call it, "My custom element." We'll make sure it extends HTML element. Inside its constructor, we'll call the super method, which makes sure we inherit everything from HTML element.

[00:22] We'll define the click event and set up a call-back that fires an alert. Now we can define a connected call-back, which fires when the custom element is connected to the dome. Here, it is safe to do dome manipulations like inner HTML. This is a good place to set up our custom elements template.

[00:43] The second thing we need to do is to define the custom elements, so window.customelements.define. We pass the custom tag name and the class we've just created. The final thing we need to do is to actually use the tag inside our dome. We set it up, refresh the page, and here is our custom element. We click it and we see that our click is registered.

[01:05] This is how we create a custom element.