Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Render Hello World Text with Custom Elements


    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.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: 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.

    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.

    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.

    This is how we create a custom element.